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

时间:2021-10-05 09:32:39

What I want?
I want to add radio button functionality where a user in particular section can only select one row

我想要的吗?我想要添加单选按钮功能,在其中,特定区域的用户只能选择一行。

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

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中获取给定部分的所有行?

P.S: I am a week old to iOS and learning, so bear with me if this is a stupid question

P。S:我已经学了一周iOS了,如果这是一个愚蠢的问题,请见谅

3 个解决方案

#1


11  

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
    }
}

#2


2  

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 !

感谢灵魂诊所的帮助!

#3


0  

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类别中。

#1


11  

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
    }
}

#2


2  

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 !

感谢灵魂诊所的帮助!

#3


0  

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类别中。