iphone uitableview 按日期分组实现
目前我有一个显示消息的表格,但我想按日期对消息进行分组,例如
Tuesday 4/5/98 //header
message 1
message 2
message 3
Wednesday 4/6/98
message 1
等等。
所以 现在,它只是一个长 NSMutableArray oldArr (已排序)。
我正在考虑做的是创建另一个唯一日期对象(DateGroup)的 NSMutableArray (groupArr),其中每个 DateGroup 将具有以下 ivars:
int size; //number of messages for date
int index; //index of first message in the total array
//so I can easily retrieve the object when the section and row is asked
NSDate date; //need the date for the header
通过这些 ivars,我可以获得具有 groupArr 大小的所有部分,所有单独的行通过访问 DateGroup 大小来调整大小,并在给定一个部分时通过访问单个单元格,通过获取索引 + 行来获取行参数。
我认为这是最好的方法。但是,我在从 oldArr 填充 groupArr 时遇到问题(其大小会动态增加)。我正在考虑用这个伪代码来逐一浏览 oldArr:
NSDate date = nil;
int size = 1;
int index = 0;
for (int i = 0; i < oldArr.size; i++) {
OldGroup* cur = [oldArr objectAt:i];
if (date is different from cur->date){ //i know, it's pseudocode
DateGroup* newGroup = [[DateGroup alloc] initWithDate:cur->date index:index];
[groupArr add:NewGroup];
date = cur->date;
index += size;
size = 1; <br/>
} else{ //the date is the same, so the object belongs in the group
[groupArr lastObject].size++;
}
}
无论如何,虽然我认为这会起作用,但对我来说似乎非常不优雅。我正在考虑使用 NSMutableArry 的“indexOfObjectPassingTest”来查找下一个日期,但似乎无法从概念上实现它。我正在尝试设计一种好方法来做到这一点。有什么建议吗?
So currently I have a table that displays messages, but I'd like to group the messages by date, e.g.
Tuesday 4/5/98 //header
message 1
message 2
message 3
Wednesday 4/6/98
message 1
etc.
So right now, it's just one long NSMutableArray oldArr (sorted).
What I was thinking of doing, was creating another NSMutableArray (groupArr) of unique date objects (DateGroup), in which each DateGroup would have the following ivars:
int size; //number of messages for date
int index; //index of first message in the total array
//so I can easily retrieve the object when the section and row is asked
NSDate date; //need the date for the header
With these ivars, I can get all the sections with groupArr size, all the individual row sizes by accessing the DateGroup size, and the individual cell when given a section, row arguments by getting the index + row.
I think this is the best way to do it. However, I am having problems populating the groupArr from the oldArr (which will dynamically increase in size). I was thinking of going one by one through the oldArr with this psuedocode:
NSDate date = nil;
int size = 1;
int index = 0;
for (int i = 0; i < oldArr.size; i++) {
OldGroup* cur = [oldArr objectAt:i];
if (date is different from cur->date){ //i know, it's pseudocode
DateGroup* newGroup = [[DateGroup alloc] initWithDate:cur->date index:index];
[groupArr add:NewGroup];
date = cur->date;
index += size;
size = 1; <br/>
} else{ //the date is the same, so the object belongs in the group
[groupArr lastObject].size++;
}
}
Anyway, while I think this will work, it seems very unelegant to me. I was thinking about using the "indexOfObjectPassingTest" of NSMutableArry to find the next date, but can't seem to implement it conceptually. I'm trying to design a good way to do this. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我什至还没有编译这个,所以要小心错误和泄漏,这可能不是最有效的方法。但我认为您可以使用 NSDateFormatter 来截断在将日期分组时不关心的时间部分。结果是一个组数组,每个组都是按日期排序的事件数组。然后你只需要一个排序谓词来按照你选择的显示顺序排列它,它就可以用于 UITableView 了。
I haven't even compiled this so beware of errors and leaks, and it is perhaps not the most efficient way. But I think you can use the NSDateFormatter to chop the time component that you don't care about when sorting dates into groups. The result is an array of groups with each group being an array of your events, sorted into dates. Then you just need a sort predicate to arrange it in the order you choose to display it and it's ready for the UITableView.