开发者

action from UITableViewCellAccessoryCheckmark

I have created a simple tableView with 5 rows. and used UITableViewCellAccessoryCheckmark on the cells.

I want to push a button and do an action based on what cells are 'Checked', and what ones aren't. The user is able to select none, all, 1, etc.

How do I check to see if/what cells are using UITableViewCellAccessoryCheckmark, and which ones are using UITableViewCellAccessoryNone ?

Thanks

Sam

EDIT!

I found a way to do this...

In didSelectRowAtIndexPath I did the following...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.

    NSUserDefaults *loginDefaults = [NSUserDefaults standardUserDefaults];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:indexPath]; 
    if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) 
    {   
        oldCell.accessoryType = UITableViewCellAccessoryNone;   

    } else {

        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];     

        if (newCell.accessoryType == UITableViewCellAccessoryNone) 
        {   
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;  
        }  
    }

    if ([oldCell.textLabel.text isEqualToString:@"All"]) {

        if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {

            [loginDefaults setObject:@"YES" forKey:@"DownloadAll"];

        } else if (oldCell.accessoryType == UITableViewCellAccessoryNone) {

            [loginDefaults setObject:@开发者_如何转开发"NO" forKey:@"DownloadAll"];
        }

    } else if

A little around the houses, but as i only have 5 things in my table view, it works well for what i want it to do.

Now in the action, i check the values in the NSUserDefaults and work accordingly


The information about which elements is checked is something that belongs in your model. As table cells are reused when you scroll, there is no way for the table itself to store it, as at any given time there will only be one or two more cells in memory than you can see on the screen.

So I suggest you add a property to your model objects and update that when a cell gets tapped. This allows you to do any filtering and querying you like on your model, without having to try to force a UI element to store your state, which it by design cannot do reliably.

If you try to anyway, you will end up with weird situations, like the checkmarks appearing in rows you did not anticipate, because the cell instance will be reused.

See also question Question 3240830, it is quite similar.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜