Search code examples
iphoneuitableviewaccessorytype

UITableViewCell with 2 accessory types


I would like to make a UITableViewCell with one label and two accessory types:

  • Unselected cells should display a UITableViewCellAccessoryDetailDisclosureButton accessory.
  • The selected cell should display both the UITableViewCellAccessoryDisclosureIndicator and the UITableViewCellAccessoryDetailDisclosureButton accessories.

The only way I know how to do this is by using an image for the selected cell's accessory view. Is there an alternative way to do this?


Solution

  • In your

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        selectedIndex = indexPath //selectedIndex is a property
    
    }
    

    Then in

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //usual cell stuff
    
        if(indexPath == selectedIndex) 
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        else
            [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
    
    
    }
    

    So the trick is just to keep a reference to the selected cell and set the indicator accordingly.

    Note that you might want to test if the cell is already selected before setting the selectedIndex, in that case you should set selectedIndex = nil.