开发者

I want to add check sign in row when any row is selected in UITableView

开发者 https://www.devze.com 2023-02-02 19:06 出处:网络
I want to add a check sign when any row is selec开发者_StackOverflow社区ted in table view please helpImplement tableView:didSelectRowAtIndexPath: in your table view delegate, grab the cell that was se

I want to add a check sign when any row is selec开发者_StackOverflow社区ted in table view please help


Implement tableView:didSelectRowAtIndexPath: in your table view delegate, grab the cell that was selected using [tableView cellForRowAtIndexPath:indexPath] and set its accessory to UITableViewCellAccessoryCheckmark.

If you want to implement something similar to the multi-value table views in the Settings app, what I do is use a property to keep track of what's already selected:

@property (nonatomic, retain) NSIndexPath *selectedIndexPath;

In the same delegate method, I remove the checkmark from the cell at the selection, add the checkmark to the cell that was tapped and update the property with the index of that cell:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.selectedIndexPath];
    [oldCell setAccessory:UITableViewCellAccessoryNone];

    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
    [newCell setAccessory:UITableViewCellAccessoryCheckmark];

    [self setSelectedIndexPath:indexPath];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

To go one step further, you can find a way to persist the index path in your app (possibly using NSUserDefaults, and in tableView:cellForRowAtIndexPath: apply the checkmark to whichever cell that corresponds to that index path, so your checkmark shows up on view load.


http://iphonedevelopment.blogspot.com/2008/10/table-view-multi-row-edit-mode.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号