在UITextView中显示表的动态标题

时间:2012-12-15 22:43:49

标签: ios uitableview sdk nsmutablearray

我有一个包含事件的数组。所有这些事件都显示在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];

非常感谢!

3 个答案:

答案 0 :(得分: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];
    }
    

答案 1 :(得分:0)

不要忘记添加

 [yourTableView reloadData];

更新tableView的内容后...

答案 2 :(得分:0)

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

假设您的标头(.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],并为您完成工作。