0

我有一个包含事件的数组。所有这些事件都显示在 UITableView 中。每次打印新的事件日期时,如何设置新的部分标题?

E.g.:

20.12.2012
- event 1
- event 2
- event 4

21.12.2012:
- event 3

22.12.2012:
-event 5

这就是我的 Event 对象的样子:

events = [NSMutableArray arrayWithCapacity:20];

Event *e1 = [[Event alloc] init];
e1.date = @"20.12.2012";
e1.time = @"15:00 Uhr";
e1.address = @"Bla";
[events addObject:e1];

Event *e2 = [[Event alloc] init];
e2.date = @"20.12.2012";
e2.time = @"16:00 Uhr";
e2.address = @"Bla";
[events addObject:e2];

Event *e3 = [[Event alloc] init];
e3.date = @"20.12.2012";
e3.time = @"16:30 Uhr";
e3.address = @"Bla";
[events addObject:e3];

Event *e4 = [[Event alloc] init];
e4.date = @"21.12.2012";
e4.time = @"17:30 Uhr";
e4.address = @"Bla";
[events addObject:e4];

非常感谢你!

4

3 回答 3

0
  1. 计算所有事件中唯一日期的数量;
  2. 实施

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
       return numberOfUniqueDates;
    }
    
  3. 实施

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [eventsData[section] count];
    }
    
  4. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        Use indexPath.section and indexPath.row to determine the values for cells;
    }
    
  5. 实施

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        return date[section];
    }
    
于 2012-12-16T04:42:48.500 回答
0

不要忘记添加

 [yourTableView reloadData];

更新 tableView 的内容后...

于 2012-12-26T07:08:27.403 回答
0

您可以将所有事件保存到以日期为键的字典中。

假设您在 header(.h) 文件中有_eventsDictionary_events作为 ivar。NSMutableDictionary *_eventsDictionary; NSArray *_events

- (void)reloadTableData {
   _eventsDictionary = [NSMutableDictionary dictionary];
   for (Event *ev in _events) {
        NSMutableArray *eventArray = [_eventsDictionary objectForKey:ev.date];
        if (! eventArray) {
            // first event for this date
            eventArray = [NSMutableArray array];
            [_eventsDictionary setObject:eventArray forKey:ev.date];
        }
        [eventArray addObject:ev];
    }
    [self.tableView reloadData];
}

现在您拥有数组中的所有事件,这些事件位于_eventsDictionary.

- (NSString *)dateStringAtIndex:(NSInteger)index {
   // Actually I prefer sort them as date objects
// Since you have all dates as string, it should ok for you
   NSArray *allSortedKeys = [[_eventsDictionary allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

   return [allSortedKeys objectAtIndex:index];
}
- (NSArray *)eventsArrayAtIndex:(NSInteger)index {

   NSArray *eventsArray = [_eventDictionary objectForKey:[self dateStringAtIndex:index]];
   return eventsArray;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _eventDictionary.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

   return [[self eventsArrayAtIndex:section] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
 return [self dateStringAtIndex:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   cell = .....
   .....

    NSArray *eventsArray = [self eventsArrayAtIndex:indexPath.section];
    Event *ev = [eventsArray objectAtIndex.row];
    // configure your cell with the event
    // eg. cell.textLabel.text = ev.time
    return cell;
}

现在,您可以在事件数组中的事件之后在某处调用 [self reloadTableData],它为您完成了这项工作。

于 2012-12-27T12:55:41.957 回答