iphone uitableview 按日期分组实现

发布于 2024-09-12 12:24:15 字数 1468 浏览 1 评论 0原文

目前我有一个显示消息的表格,但我想按日期对消息进行分组,例如

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几味少女 2024-09-19 12:24:15

我什至还没有编译这个,所以要小心错误和泄漏,这可能不是最有效的方法。但我认为您可以使用 NSDateFormatter 来截断在将日期分组时不关心的时间部分。结果是一个组数组,每个组都是按日期排序的事件数组。然后你只需要一个排序谓词来按照你选择的显示顺序排列它,它就可以用于 UITableView 了。

- (void)addDatedEvent:(MyEventClass*)newEvent ToGroup:(NSMutableArray*)dateGroups 
{
  NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
  [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
  [dateFormatter setDateStyle:NSDateFormatterMediumStyle];

  NSDate* eventDate = newEvent.evDate;
  NSString* dateString = [dateFormatter stringFromDate:eventDate];

  BOOL added = NO;
  for(NSMutableArray* group in dateGroups)
  {
    MyEventlass* firstEvent = [group objectAtIndex:0];
    NSDate* firstEventDate = firstEvent.evDate;
    NSString* firstEventDateString = [dateFormatter stringFromDate:firstEventDate];

    if([firstEventDateString isEqualToString:dateString])
    {
      // match - this event joins others in an existing group
      [group addObject:newEvent];
      added = YES;
    }
  }

  if(added == NO)
  {
    // need to create a new group since this is the first date
    NSMutableArray* newGroupArray = [NSMutableArray arrayWithObject:newEvent];
    [dateGroups addObject:newGroupArray];
  }
}

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.

- (void)addDatedEvent:(MyEventClass*)newEvent ToGroup:(NSMutableArray*)dateGroups 
{
  NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
  [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
  [dateFormatter setDateStyle:NSDateFormatterMediumStyle];

  NSDate* eventDate = newEvent.evDate;
  NSString* dateString = [dateFormatter stringFromDate:eventDate];

  BOOL added = NO;
  for(NSMutableArray* group in dateGroups)
  {
    MyEventlass* firstEvent = [group objectAtIndex:0];
    NSDate* firstEventDate = firstEvent.evDate;
    NSString* firstEventDateString = [dateFormatter stringFromDate:firstEventDate];

    if([firstEventDateString isEqualToString:dateString])
    {
      // match - this event joins others in an existing group
      [group addObject:newEvent];
      added = YES;
    }
  }

  if(added == NO)
  {
    // need to create a new group since this is the first date
    NSMutableArray* newGroupArray = [NSMutableArray arrayWithObject:newEvent];
    [dateGroups addObject:newGroupArray];
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文