1

我有一个在表格视图中加载的数组,如果用户点击某个单元格,它会更改为 UITableViewCellAccessoryCheckmark。如何检查数组中的哪些对象被检查并将所有检查的对象添加到另一个数组?

4

2 回答 2

1

If you want a function that actually gets the checked objects at a whim, use the following:

- (NSMutableArray*)checkedObjectsInTable:(UITableView*)tableView
{
    NSMutableArray *checkedObjects = [[[NSMutableArray alloc] init] autorelease];
    for (int i=0; i<tableDataSource.count; i++)
        {
            if ([tableView cellForRowAtIndexPath:
                 [NSIndexPath indexPathForRow:i inSection:0]].accessoryType == UITableViewCellAccessoryCheckmark)
            {
                [checkedObjects addObject:[tableDataSource objectAtIndex:i]];
            }
        }

    return checkedObjects;
}

That would allow you to get the data on demand. Note that it would be much less efficient than simply using Jasarien's method, yet there are some situations where it is a better solution.

于 2011-09-01T00:32:53.723 回答
1

在你的tableView:didSelectRowAtIndexPath:方法中是这样的:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //set checkmark accessory on table cell ...

    // get object and add to checkedObjects array
    NSInteger index = [indexPath row];
    MyObject *object = [myArray objectAtIndex:index];
    [checkedObjects addObject:object];
}
于 2011-09-01T00:18:32.980 回答