简体   繁体   English

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

[英]UITableView: How to get all rows for a particular section?

What I want? 我想要的是?
I want to add radio button functionality where a user in particular section can only select one row 我想添加单选按钮功能,其中特定部分的用户只能选择一行

在此输入图像描述

How am I doing it? 我怎么做的?
I added UISwitch and based on what row user click I would like to do the following 我添加了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          
    }
}

Where am I stuck? 我被困在哪里?

How do I get all the rows for a given section in UITableView ? 如何获取UITableView给定部分的所有行?

PS: I am a week old to iOS and learning, so bear with me if this is a stupid question PS:我已经有一周的时间去iOS和学习了,所以如果这是一个愚蠢的问题请耐心等待

Use[tableView cellForRowAtIndexPath:(NSIndexPath *)] 使用[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
    }
}

Swift 3: 斯威夫特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)
    }

Thanks @Soul Clinic for help with this ! 感谢@Soul Clinic的帮助!

This should do the trick. 这应该可以解决问题。

- (NSArray *)tableView:(UITableView *)tableView visibleCellsInSection:(NSInteger)section
{
    NSPredicate *visibleCellsInSectionPredicate = [NSPredicate predicateWithBlock:^BOOL(UITableViewCell *visibleCell, NSDictionary *bindings) {
        return [tableView indexPathForCell:visibleCell].section == section;
    }];
    return [tableView.visibleCells filteredArrayUsingPredicate:visibleCellsInSectionPredicate];
}

You can put it in a UITableView category if you want. 如果需要,可以将其放在UITableView类别中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM