簡體   English   中英

獲取包含多個部分的當前 indexPath 行?

[英]Get current indexPath row with multiple sections?

我有一個包含多個部分的 UITableView。 我使用的是一個數組,而不是每節使用一個數組的典型方法。 無論如何,我在獲取當前的 indexPath.row 時遇到了麻煩,就好像 tableview 中只有一個部分一樣。

所以幾乎我想要做的如下。 如果section == 0,我只使用indexPath.row,但是如果section> 0,我想把上一個section的所有行加起來,得到當前section的當前行,得到總行號AS如果桌面只有一個部分。

這是我當前的代碼,它只是不起作用,也許你們會看到我做錯了什么。 請告訴我:

if (indexPath.section == 0) {
        currentRow = indexPath.row;
    } else {
        NSInteger sumSections = 0;
        for (int i = 0; i < indexPath.section; i++) {
            int rowsInSection = [activitiesTable numberOfRowsInSection:i] + 1;
            sumSections += rowsInSection;
        }
        NSInteger row = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section].row + 1;
        currentRow = sumSections + row;
    }

首先你不需要第一個 if 因為它不會通過 for-next 運行。 然后你添加一個不必要的,然后你可以像這樣添加 indexPath.row :

    NSInteger sumSections = 0;
    for (int i = 0; i < indexPath.section; i++) {
        int rowsInSection = [activitiesTable numberOfRowsInSection:i];
        sumSections += rowsInSection;
    }
    currentRow = sumSections + indexPath.row;

這是 swift 4 的版本。

var currentIndex = 0
var sumSections = 0;

for index in 0..<indexPath.section {
    let rowsInSection = self.tableView.numberOfRows(inSection: index)
    sumSections += rowsInSection
}

currentIndex = sumSections + indexPath.row;

這是我目前在 Swift 4 中使用的對我有用的方法

var currentRow = 0
for section in 0..<indexPath.section {
        let rows = self.tableView.numberOfRows(inSection: section)
        currentRow += rows
}

currentRow += indexPath.row

暫無
暫無

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

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