如何按日期划分UITableView的部分

时间:2015-02-03 14:34:23

标签: ios objective-c xcode date tableview

我有一个分为单元格的TableView。单元格的文本是fireDate到UILocalNotification。这是我的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [[[UIApplication sharedApplication]scheduledLocalNotifications]count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{    
    cellView *cell = (cellView *)[tableView dequeueReusableCellWithIdentifier: @"Cell" forIndexPath:indexPath];
    NSArray *localnotifications = [[UIApplication sharedApplication]scheduledLocalNotifications];
    UILocalNotification *localNotification = [localnotifications objectAtIndex:indexPath.row];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM/dd/yyyy"];
    NSString *timeString = [formatter stringFromDate:localNotification.fireDate];
    [cell.textlabel setText:timeString];
    return cell;
}

如何创建按日期划分的部分?

1 个答案:

答案 0 :(得分:0)

  

首先,你的节号必须是数组的计数

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {     //返回部分的数量。     return [[[UIApplication sharedApplication] scheduledLocalNotifications] count]; }
  

行数必须是每个部分一个

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 1;
}
  

这里给出了每个部分的名称,我不知道你的结构   数组,但如果它是一个字典,例如:objectForKey:@" date" ...

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [NSString stringWithFormat:@"%@", [[[UIApplication sharedApplication]scheduledLocalNotifications] objectAtIndex:section];
}
  

在cellForRowAtIndexPath中首先检查indexPath.section并设置   像往常一样用你的对象

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{    
    switch (indexPath.section) {
        case indexPath.row:
       // fill your cell // 
        default:
        break;
    }

}

我没有靠近我的计算机来测试我是通过记忆告诉你的,我希望至少你知道tableView是如何工作的。

祝你好运。