UITableView 部分问题 [英] UITableView Problem with Sections

查看:46
本文介绍了UITableView 部分问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写出了将 NSMutableArray 划分为 UITableView 的部分的所有代码.但是在 cellForRowAtIndexPath 我发现 indexPath.row 只返回当前部分的行.有什么方法可以简单地提供整个 UITableView 的行数.如果有人需要查看代码,我会附上它,但我认为回答这个问题不是很有用,除非我必须使用另一种算法来查找 cellForRow.

I have all the code written out to divide an NSMutableArray into sections for a UITableView. But in cellForRowAtIndexPath I am finding that indexPath.row returns only the row for the current section. Is there any way to simply provide the number of the row for the entire UITableView. If anyone needs to see the code I will attach it but I don't think it's very useful to answer this question except for if I must use another algorithm to find the cellForRow.

推荐答案

是的,indexPathrow 属性就是部分中的行,所以你需要存储一个数组数组(每个部分的数组).类似的东西:

Yep, the row property of the indexPath is the row in the section, so you need to store an array of arrays (an array for each section). Something like:

// an array of arrays, one for each section
NSArray * _sectionDataArray;

当您建立索引时,您会为每个部分创建一个数组并将其存储在 _sectionDataArray 中.为此,您需要构建一个 NSMutableDictionary 对象,该对象由您的部分标题键控,其中包含一个 CellThing 数组(一个包含您的单元格数据的对象).一旦你有了字典,你就可以抓取每个字母的数组并将其粘贴到 _sectionDataArray 中.

When you build you index you create an array for each section and store it in the _sectionDataArray. To do this you need to build a NSMutableDictionary object, keyed by your section headers, containing an array of CellThing (an object containing your cell data). Once you have a dictionary you can just grab the array for each letter and stick it into _sectionDataArray.

在您的 cellForRowAtIndexPath 方法中,您可以通过获取像这样的节数组来访问单元格数据:

In your cellForRowAtIndexPath method, you can access the cell data by getting the section array like this:

NSArray * sectionArray = [_sectionDataArray objectAtIndex:indexPath.section];

一旦你有了节数组,你就可以通过使用 indexPath 中的 row 来访问你想要的单个单元格数据:

Once you have the section array you can access the individual cell data you are after by using the row in the indexPath:

CellThing * cellObject = [sectionArray objectAtIndex:indexPath.row];

这篇关于UITableView 部分问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆