简体   繁体   中英

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?

Make a custom UITableViewCell (numerous tutorials and examples online and also in the documentation ).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM