简体   繁体   English

UITableView,我如何知道cellForRowAtIndexPath期间的哪个部分?

[英]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.sectionindexPath.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.

相关问题 uitableview cellforrowatindexpath未调用,但没有:调用section中的行 - uitableview cellforrowatindexpath not called but no:of rows in section is called 如何在UITableView中的每个部分放置一个复选标记? - How do I put one check mark per section in UITableView? 如何将 UITableView 滚动到不包含行的部分? - How do I scroll a UITableView to a section that contains no rows? 您如何知道UITableView节标题上的NSString(使用默认字体)将占用多少空间? - How do you know how much space an NSString (using default font) will occupy on a UITableView section header? 我在UITableView,cellForRowAtIndexPath中得到重复 - I am getting duplicates in UITableView, cellForRowAtIndexPath 带有节和已解析数据的UITableView如何创建cellForRowAtIndexPath - UITableView with Sections and Parsed Data how to create cellForRowAtIndexPath 在获取 UITableView 部分和行之后……这段代码有什么作用? - After fetching UITableView Section and Row… what does this code do? 如何在UITableView上浮动所有节标题视图(或保持所有可见)? - How do I float all section header views (or keep all visible) on a UITableView? 我如何像UITableView一样继承UIScrollView? - How can I inherit a UIScrollView like what the UITableView do? 如何在UITableView中隐藏一个部分? - How to hide a section in UITableView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM