簡體   English   中英

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

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

我想要的是?
我想添加單選按鈕功能,其中特定部分的用戶只能選擇一行

在此輸入圖像描述

我怎么做的?
我添加了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          
    }
}

我被困在哪里?

如何獲取UITableView給定部分的所有行?

PS:我已經有一周的時間去iOS和學習了,所以如果這是一個愚蠢的問題請耐心等待

使用[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
    }
}

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

感謝@Soul Clinic的幫助!

這應該可以解決問題。

- (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];
}

如果需要,可以將其放在UITableView類別中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM