简体   繁体   English

iOS indexpath.row和indexpath.section

[英]iOS indexpath.row and indexpath.section

So I have a UITableView with a contact list set up. 所以我有一个设置了联系人列表的UITableView All the contacts are broken up into sections based on their first name and ordered alphabetically. 所有联系人根据其名字分为几部分,并按字母顺序排列。 I'm trying to get it so people select who they want to add. 我正在尝试让人们选择他们想要添加的人。 In my didSelectRowAtIndexPath method, when I use indexPath.section or indexPath.row I keep getting the wrong results. 在我的didSelectRowAtIndexPath方法中,当我使用indexPath.section或indexPath.row时,我总是得到错误的结果。 Below are the results I get for using .row and .section. 以下是我使用.row和.section得到的结果。

Name | Section | Row | Cell index
---------------------------------
Adi  | 0       | 0   | 0
Andy | 0       | 1   | 1
Ben  | 1       | 0   | 2
Jay  | 2       | 0   | 3
Jim  | 2       | 1   | 4
Joe  | 2       | 3   | 5

Is there anyway I can get the actual cell index to use the didSelectRowAtIndexPath method? 无论如何,我可以获取实际的单元didSelectRowAtIndexPath使用didSelectRowAtIndexPath方法吗?

This is absolutely correct. 这是绝对正确的。 The index path contains row indexes range 0...n PER index section (also range 0...n ). 索引路径包含行索引范围为0...n PER的索引段(也为范围0...n )。

This allows you to create an array structure like 这使您可以创建类似的数组结构

@(@(@"section 0", @"section 0" @"section 0"), @(@"section 1", @"section 1"))

Option 1: Set up your data as an array of arrays. 选项1:将数据设置为数组数组。 Each element in the top array matches a section and each element in a section array matches a row. 顶部数组中的每个元素都匹配一个节,而节数组中的每个元素都匹配一个行。

Option 2: Write a method that calculates your cell index for a section and row by adding up all the rows in previous sections and adjusting for the row in the index path. 选项2:编写一种方法,通过将前面各节中的所有行加起来并针对索引路径中的行进行调整,从而计算出该节和某行的单元格索引。

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

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