Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a UITableViewCell accessory type on some cells, but not all

I have 8 cells that are being built in my UITableViewController. I would like to know how I can show a disclosure indicator on the 4th and 8th cells. Right now I am building it in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

though I am fully aware it is going to add a disclosure indicator to every cell

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
like image 413
Sheehan Alam Avatar asked Mar 29 '10 22:03

Sheehan Alam


2 Answers

Use a simple if statement:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ... // do whatever
    UITableViewCellAccessoryType accessoryType = UITableViewCellAccessoryNone;
    if (indexPath.row == 3 || indexPath.row == 7) {
        accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.accessoryType = accessoryType;
    ... // do whatever
}
like image 83
indragie Avatar answered Oct 14 '22 21:10

indragie


You know the indexPath, so why not just conditionally apply the disclosure indicator when

if((indexPath.row == 3) || (indexPath.row == 7))

?

(indexPath.row is 0-based of course... the 4th is 3 and the 8th is 7. And you should use #defines or some other way to keep the magic numbers out of there too.)

like image 33
Adam Eberbach Avatar answered Oct 14 '22 20:10

Adam Eberbach