[英]UITableView, how do I know what Section during cellForRowAtIndexPath?
I have a UITableView displaying a list of Cities. 我有一个显示城市列表的UITableView。 I want to separate them by State.
我想用国家把它们分开。 I can't seem to figure out how to get it to pick the right items out of my Array.
我似乎无法弄清楚如何从我的阵列中选择正确的项目。 If Section 1 (Arizona) has 2 Cities and Section 2 (California) has 2 Cities, during cellForRowAtIndexPath, Section 2, City 1 has an index of 0, even though it's the 3rd item in my array.
如果第1节(亚利桑那州)有2个城市而第2节(加利福尼亚州)有2个城市,则在cellForRowAtIndexPath第2节中,城市1的索引为0,即使它是我的数组中的第3个项目。 I thought about just turning my City Array into a State Array, where each item holds an Array of Cities, but I still don't know what section I'm on and therefore don't know which City Array under the States Array I would need to access.
我想过把我的城市阵列变成一个状态阵列,每个项目都有一个城市阵列,但我仍然不知道我在哪个部分,因此不知道状态数组下的哪个城市数组我会需要访问。
The method is called 该方法被调用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
The indexpath parameter contains a row and section property. indexpath参数包含row和section属性。
indexPath.section
indexPath.row
Here's documentation link for the NSIndexPath class. 这是NSIndexPath类的文档链接。
您可以使用indexPath.section
和indexPath.row
Swift 3, 4 and 4.2 斯威夫特3,4和4.2
Using Switch
使用Switch
switch indexPath.section {
case 0:
print("Write your code for section number one.")
case 1:
print("Write you code for section number two")
default:
return
}
Using if else
使用if else
if indexPath.section == 0 {
print("Write your code for section number one.")
} else if indexPath.section == 1 {
print("Write your code for section number two.")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.