ios 获取多个部分的 UITableview 的行号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16968602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get row number of UITableview with multiple sections
提问by hyd00
I have a UITableView, with multiple sections in it, and each section has multiple rows. I want to get the row number of selected cell with respect to the entire table and not the section only.
我有一个UITableView, 里面有多个部分,每个部分都有多行。我想获取与整个表格相关的选定单元格的行号,而不仅仅是该部分。
example:
例子:
- I have two sections in the
UITableView, section 1 has 3 rows and section 2 has 5 rows. - When I select sections 2's 2nd row, I should get the 5 as the row number in
didSelectRowAtIndexPathmethod, rather than getting 2 as the row number (which is with respect to the section).
- 我在 中有两个部分
UITableView,第 1 部分有 3 行,第 2 部分有 5 行。 - 当我选择第 2 节的第 2 行时,我应该将 5 作为
didSelectRowAtIndexPath方法中的行号,而不是将 2 作为行号(这是相对于该节的)。
I tried to get the row number myself by doing the following, but it does not seem to work:
我尝试通过执行以下操作来自己获取行号,但似乎不起作用:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%d",indexPath.row);
int theRow = indexPath.row;
NSLog(@"%d",theRow);
}
I was thinking of storing the row number in an intvariable, and then add row numbers to it myself, but the code crashes when trying to store indexpath.rowin theRow.
我想存储的行号的int变量,然后添加行号,以它自己,但尝试存储时的代码崩溃 indexpath.row的theRow。
Please help. Thank you
请帮忙。谢谢
回答by Wain
NSInteger rowNumber = 0;
for (NSInteger i = 0; i < indexPath.section; i++) {
rowNumber += [self tableView:tableView numberOfRowsInSection:i];
}
rowNumber += indexPath.row;
回答by PJeremyMalouf
Here is a Swift adaption of the above answer by Wain
这是 Wain 对上述答案的 Swift 改编
class func returnPositionForThisIndexPath(indexPath:NSIndexPath, insideThisTable theTable:UITableView)->Int{
var i = 0
var rowCount = 0
while i < indexPath.section {
rowCount += theTable.numberOfRowsInSection(i)
i++
}
rowCount += indexPath.row
return rowCount
}
回答by Max Masnick
Here's a cleaner implementation of this concept in Swift:
这是这个概念在 Swift 中的一个更清晰的实现:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
var rowNumber = indexPath.row
for i in 0..<indexPath.section {
rowNumber += self.tableView.numberOfRowsInSection(i)
}
// Do whatever here...
}
回答by SirRupertIII
I had a big data set and the previous answers using for loops was causing performance issues for me in the lower sections. I ended up doing the calculating beforehand and sped things up a bit.
我有一个大数据集,之前使用 for 循环的答案在下面的部分对我造成了性能问题。我最终预先进行了计算并加快了速度。
private var sectionCounts = [Int:Int]()
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let number = fetchedResultsController.sections?[section].numberOfObjects ?? 0
if section == 0 {
sectionCounts[section] = number
} else {
sectionCounts[section] = number + (sectionCounts[section-1] ?? 0)
}
return number
}
func totalRowIndex(forIndexPath indexPath: NSIndexPath) -> Int {
if indexPath.section == 0 {
return indexPath.row
} else {
return (sectionCounts[indexPath.section-1] ?? 0) + indexPath.row
}
}
回答by Jakub Truhlá?
Let say I implement the code below in didSelectRowAt/ didSelectItemAtcallbacks ...
假设我在didSelectRowAt/ didSelectItemAtcallbacks 中实现了下面的代码......
UITableView
用户界面
var index: Int = indexPath.row
for i in 0..<indexPath.section {
index += collectionView.numberOfRows(inSection: i)
}
UICollectionView
用户界面集合视图
var index: Int = indexPath.item
for i in 0..<indexPath.section {
index += collectionView.numberOfItems(inSection: i)
}
Example: 2 sections, 3 rows(items) each. Selected row(item) 1 in the section 1,
index= 4
示例:2 个部分,每个部分 3 行(项目)。在第 1 部分中选定的行(项目)1,
index= 4
回答by Smit
i dont know about the multiple sections but i can give you for the one section...
我不知道多个部分,但我可以给你一个部分...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger index=indexPath.row;
NSString *string=[[NSString alloc]initWithFormat:@"%ld",(long)index];
}
from this you can get the row number and you can save it to the string....
从中您可以获得行号,并且可以将其保存到字符串中....

