Hiding a cell

General discussion about iOS development - a great place to ask questions about iOS or help out others. For discussion of the official tutorials, see the Official Tutorials forum.

Hiding a cell

Postby Mr.Dublin » Tue May 29, 2012 10:40 pm

Experts

I am using a table view controller with static cells... I am populating the cells from a web service...in some cases some of those cells would be nil ...is it possible to hide those cells??

Thanks
Mr.Dublin
Hacker
 
Posts: 14
Joined: Fri Jan 13, 2012 2:38 pm
Has thanked: 1 time
Been thanked: 0 time

Re: Hiding a cell

Postby marciokoko » Wed May 30, 2012 12:37 am

You mean

Cell at index path.row = 1 has a value "bob"
Cell at index path.row = 2 has a value "pete"
Cell at index path.row = 3 has a value nil
Cell at index path.row = 4 has a value "Mary"

Therefore

Cell at index path.row = 1 has a value "bob"
Cell at index path.row = 2 has a value "pete"
Cell at index path.row = 3 has a value "Mary"

Or

Cell at index path.row = 1 has a value "bob"
Cell at index path.row = 2 has a value "pete"
Cell at index path.row = 3 has a value " "
Cell at index path.row = 4 has a value "Mary"
--------------
"Stay Hungry, Stay Foolish!"
Mars
santiapps.com
@santiapps
MBA-2011-iPhone5s-iPad3
User avatar
marciokoko
Team Member
Forum Subject Matter Expert
 
Posts: 2209
Joined: Wed Jun 08, 2011 9:37 pm
Has thanked: 4 times
Been thanked: 313 times

Re: Hiding a cell

Postby Mr.Dublin » Wed May 30, 2012 7:38 am

I meant for the case of
Cell at index path.row = 1 has a value "bob"
Cell at index path.row = 2 has a value "pete"
Cell at index path.row = 3 has a value nil
Cell at index path.row = 4 has a value "Mary"

I want to get:

Cell at index path.row = 1 has a value "bob"
Cell at index path.row = 2 has a value "pete"
Cell at index path.row = 3 has a value "Mary"

but is is possible to keep the row index values so that I get:

Cell at index path.row = 1 has a value "bob"
Cell at index path.row = 2 has a value "pete"
Cell at index path.row = 4 has a value "Mary"
Mr.Dublin
Hacker
 
Posts: 14
Joined: Fri Jan 13, 2012 2:38 pm
Has thanked: 1 time
Been thanked: 0 time

Re: Hiding a cell

Postby marciokoko » Wed May 30, 2012 1:22 pm

If you want to keep the index path its easier, simply check to see if the value you got is nil and if so, add a space:

If ([array objectAtIndex:indexPath.row] = nil) {
Cell.textlabel.text = @"";
} else {
Cell.textlabel.text = [array objectAtIndex:indexPath.row]
}
--------------
"Stay Hungry, Stay Foolish!"
Mars
santiapps.com
@santiapps
MBA-2011-iPhone5s-iPad3
User avatar
marciokoko
Team Member
Forum Subject Matter Expert
 
Posts: 2209
Joined: Wed Jun 08, 2011 9:37 pm
Has thanked: 4 times
Been thanked: 313 times

Re: Hiding a cell

Postby Mr.Dublin » Wed May 30, 2012 2:33 pm

marciokoko wrote:If you want to keep the index path its easier, simply check to see if the value you got is nil and if so, add a space:

If ([array objectAtIndex:indexPath.row] = nil) {
Cell.textlabel.text = @"";
} else {
Cell.textlabel.text = [array objectAtIndex:indexPath.row]
}



If I add a space would this hide the cell?? I do not want the user to see empty cell...
Mr.Dublin
Hacker
 
Posts: 14
Joined: Fri Jan 13, 2012 2:38 pm
Has thanked: 1 time
Been thanked: 0 time

Re: Hiding a cell

Postby marciokoko » Wed May 30, 2012 2:45 pm

oh ok! No it would not hide the cell. It would present an empty one.

I don't believe you can hide it all together. What you could do is keep a snapshot of your current index paths identifying that cell as being empty. Eliminate it for the time being. And whenever you need it again, give it the old index path.
--------------
"Stay Hungry, Stay Foolish!"
Mars
santiapps.com
@santiapps
MBA-2011-iPhone5s-iPad3
User avatar
marciokoko
Team Member
Forum Subject Matter Expert
 
Posts: 2209
Joined: Wed Jun 08, 2011 9:37 pm
Has thanked: 4 times
Been thanked: 313 times

Re: Hiding a cell

Postby rcasey » Wed May 30, 2012 3:05 pm

Try setting the height of the cell to 0 with the heightForRowAtIndexPath method.
Code: Select all
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (  << cell at indexPath.row is nil >> ) // insert your logic for determining if cell is nil
        return 0;
    else
        return 44;  // or your normal cell height
}


I haven't looked to see if this technique is going to break anything else! :)

If you are using editing mode for this table view, you'll need to add similar logic to methods like canEditRowAtIndexPath, canMoveRowAtIndexPath, etc.

Have fun!
rcasey
Team Member
Forum Subject Matter Expert
 
Posts: 1189
Joined: Fri Jul 01, 2011 6:50 pm
Has thanked: 2 times
Been thanked: 259 times

Re: Hiding a cell

Postby Mr.Dublin » Wed May 30, 2012 7:24 pm

rcasey wrote:Try setting the height of the cell to 0 with the heightForRowAtIndexPath method.
Code: Select all
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (  << cell at indexPath.row is nil >> ) // insert your logic for determining if cell is nil
        return 0;
    else
        return 44;  // or your normal cell height
}


I haven't looked to see if this technique is going to break anything else! :)

If you are using editing mode for this table view, you'll need to add similar logic to methods like canEditRowAtIndexPath, canMoveRowAtIndexPath, etc.

Have fun!


it is working....

Thanks
Mr.Dublin
Hacker
 
Posts: 14
Joined: Fri Jan 13, 2012 2:38 pm
Has thanked: 1 time
Been thanked: 0 time

Re: Hiding a cell

Postby aeberbach » Thu May 31, 2012 12:44 am

I would "hide" a cell by not having it in the tableview at all.

- get information from web service, get elements to display in an array in the order you want them displayed
- prepare a secondary array that will be the source for the tableview, copying all non-nil elements
- display tableview

Whenever the data source changes you can perform step 2 again and call [tableview reloadData]. I don't like the idea of having an invisible cell much.
User avatar
aeberbach
Team Member
Forum Subject Matter Expert
 
Posts: 1342
Joined: Sat Feb 19, 2011 5:37 am
Has thanked: 13 times
Been thanked: 168 times


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 8 guests