UITableView是否具有包含所有单元格的数组属性?

时间:2015-05-11 17:19:32

标签: ios objective-c uitableview

在我的一个方法中,我想在我拥有的单元格之间进行迭代并对它们进行更改,例如:

for (UITableViewCell *cell in ___________) {

    cell.accessoryType = accessoryType = UITableViewCellAccessoryCheckmark;
}

那么是否有一个属性可以完成____________?

TNX!

5 个答案:

答案 0 :(得分:2)

只有可见的单元格数组self.tableView.visibleCells

for (UITableViewCell *cell in self.tableView.visibleCells) {

    cell.accessoryType = accessoryType = UITableViewCellAccessoryCheckmark;
}

答案 1 :(得分:2)

UITableView本身对其内容了解不多。这是dataSource代表的工作。请参阅UITableViewDataSource

的文档

而不是以线性方式思考问题。将问题视为事件驱动。如果要在显示单元格之前修改单元格,可以在UITableViewDelegate上查看tableView:willDisplayCell:forRowAtIndexPath:

修改:鉴于您要设置accesoryType,您应该尝试使用tableView:cellForRowAtIndexPath:。因为那时您要么将可重复使用的单元格出列,要么手动alloc] init]出一个单元格。

答案 2 :(得分:0)

您是否尝试过:yourTableView.visibleCells

答案 3 :(得分:0)

Chikabuz是对的。

但是你应该避免像这样对细胞进行更改。相反,您应该告诉tableView您的单元格已更新,并提供- [id<UITableViewDataSource> tableView:cellForRowAtIndexPath:]中的更改,如下所示:

- (IBAction)toggleSelecting:(id)sender {
    [[self tableView] setEditing:![[self tableView] isEditing] animated:YES];
//  or with custom use with own BOOL property:
//  [self setEditing:![self editing]];
    [[self tableView] reloadRowsAtIndexPaths:[[self tableView] indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationFade];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//  cell setup here...

//  or with custom use with own BOOL property:
//  if ([self editing]) {
    if ([[tableView] isEditing]) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }

    return cell;
}

答案 4 :(得分:0)

可能你不能将所有细胞都放在一个阵列中。但您可以尝试使用以下内容:

let cellsArray: []

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //Your data source methods
    let cell = ......
    ....

    cellsArray.append(cell)
}

这样,您可以在视图加载后获取单元格数组。我认为没有其他办法可以做到这一点。