UITableView:如何获取特定部分的所有行?

day*_*mer 5 objective-c uitableview ios

我想要的是?
我想添加单选按钮功能,其中特定部分的用户只能选择一行

在此输入图像描述

我怎么做的?
我添加UISwitch并根据用户点击的行我想要执行以下操作

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1 || indexPath.section == 2) {

      // disable all the rows of this section except indexPath.row          
    }
}
Run Code Online (Sandbox Code Playgroud)

我被困在哪里?

如何获取给定部分的所有行UITableView

PS:我已经有一个星期的时间来iOS学习,所以如果这是一个愚蠢的问题请耐心等待

Sou*_*nic 13

使用[tableView cellForRowAtIndexPath:(NSIndexPath*)]

for (int i = 0; i < [self.tableView numberOfRowsInSection:indexPath.section]; i++) {
    if (i != indexPath.row) {
        UITableViewCell* cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:indexPath.section]];
        //Do your stuff
    }
}
Run Code Online (Sandbox Code Playgroud)


Tun*_*Fam 5

斯威夫特3:

let rowsCount = self.tableView.numberOfRows(inSection: indexPath.section)
    for i in 0..<rowsCount  {
        let cell = self.tableView.cellForRow(at: IndexPath(row: i, section: indexPath.section)) as! UITableViewCell
        // your custom code (deselecting)
    }
Run Code Online (Sandbox Code Playgroud)

感谢@Soul Clinic对此提供的帮助!