2013-05-16 50 views
1

我有一个静态的UITableView,有很多节。其中一个包含很多细胞,这些细胞将成为选项(点击勾选)。在单个节中循环静态单元格。 UITableView

我有一个NSMutableArray(self.checkedData),它包含所选行的行ID。我无法弄清楚如何循环特定部分中的单元格。它是检查行是否在数组中,如果是的话添加一个复选标记。所以当视图加载时,可以从coredata中拉取选项,然后标记选定的行。

我目前有这个处理添加复选标记。这工作正常。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // determine the selected data from the IndexPath.row 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    // determine the data from the IndexPath.row 

    if (![self.checkedData containsObject:[NSNumber numberWithInt:indexPath.row]]) 
    { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     [self.checkedData addObject:[NSNumber numberWithInt:indexPath.row]]; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     [self.checkedData removeObject:[NSNumber numberWithInt:indexPath.row]]; 
    }  

    [tableView reloadData]; 
} 

回答

3

你可以得到所有单元阵列中的特定部分是这样的:

NSUInteger section = 0; 
NSInteger numberOfRowsInSection = [self.tableView numberOfRowsInSection:section]; 

NSMutableArray *cellsInSection = [NSMutableArray arrayWithCapacity:numberOfRowsInSection]; 

for (NSInteger row = 0; row < numberOfRowsInSection; row++) 
{ 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section]; 

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 

    [cellsInSection addObject:cell]; 
} 

cellsInSection数组现在包含所有在该部分的细胞0

+0

谢谢。我不太确定如何去做,但这是有道理的。 –

+1

如果此时此刻不可见,则cell可以在ios7上为null。 –

+2

是的,它可以。如果你真的需要访问配置的单元格实例,你可以在表视图的数据源上调用'[tableViewDataSource tableView:tableView cellForRow ...]'方法而不是['tableView cellForRow ...]你非零的单元格的价格(可能无用)初始化/出队/配置单元格 –

1

也许这样的事情在viewDidLoad中:

for(NSIndexPath *thisIndexPath in [self.tableView indexPathsForVisibleRows]) { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    if (![self.checkedData containsObject:[NSNumber numberWithInt:indexPath.row]]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     [self.checkedData addObject:[NSNumber numberWithInt:indexPath.row]]; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     [self.checkedData removeObject:[NSNumber numberWithInt:indexPath.row]]; 
    } 
    [self.tableView reloadData]; 
} 
+0

欣赏你的帮助。感觉另一个更适合我想要的,但我也明白你的答案。谢谢。 –

相关问题