ios - 在 iPhone 中使用数组动态创建 tableView 部分

标签 ios uitableview

我有一个应用程序,我想在其中显示 TableView 部分中的数据,问题是我不知道总部分名称,因为它取决于数组。那么如何在索引路径的行单元格中显示每个部分的数据。

NSArray *test=[NSArray arrayWithObjects:@"June 20",@"July 20","May 1",@"May 10",nil];

所以我想按日期显示 tableView 部分中的数据,例如 6 月 20 日,6 月 20 日的所有记录应显示在一个部分中,而 5 月 1 日,5 月的所有记录都应显示在一个部分中。知道如何解决这个问题。谢谢

最佳答案

最好创建一个标准结构,例如(个人,我建议如下):

  1. 包含多个字典的数组
  2. 每个字典包含 2 个键
    • Day 是一个,比方说,6 月 20 日,它是
    • Events 是一个,它的值是一个数组对象
      • 这个数组是字符串的集合,基本上就是这几天的内容

示例:

可能的 UITableViewController 子类方法

- (void)viewDidLoad
{
    [super viewDidLoad];

    arrTest = @[
                @{@"Day":@"June 20",
                  @"Events":@[@"Repair window pane", @"Buy food for Elephant", @"Pay credit card dues"]},
                @{@"Day":@"July 20",
                  @"Events":@[@"Repair window frame", @"Buy alot more food for Elephant", @"Pay credit card dues dues"]},
                @{@"Day":@"May 1",
                  @"Events":@[@"Replace entire window", @"Sell Elephant, Get Cat", @"Return credit card"]},
                @{@"Day":@"May 10",
                  @"Events":@[@"Take bath", @"Shave", @"Get new credit card"]}
                ];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //get count of main array (basically how many days)
    return arrTest.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //get count of number of events in a particular day

    //old style
    //return [[[arrTest objectAtIndex:section] objectForKey:@"Events"] count];

    return [arrTest[section][@"Events"] count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    //get value of the "Day" key (June 20 / July 20 ...)

    //old style
    //return [[arrTest objectAtIndex:section] objectForKey:@"Day"];

    return arrTest[section][@"Day"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    //old style
    //NSString *strDayEvent = [[[arrTest objectAtIndex:indexPath.section] objectForKey:@"Events"] objectAtIndex:indexPath.row];

    NSString *strDayEvent = arrTest[indexPath.section][@"Events"][indexPath.row];
    [cell.textLabel setText:strDayEvent];

    return cell;
}

关于ios - 在 iPhone 中使用数组动态创建 tableView 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23210442/

相关文章:

ios - 如何根据 UITableView 中的文本增加 UILabel 高度?

ios - 从 TableView 为索引部分和行传递正确的 Realm 对象时出现问题

iphone - iOS iphone 解压缩跨区(多卷).7z 或 .zip 文件

ios - 删除时该行不会在 UITableView 中消失

删除后核心数据崩溃的 iOS UITableView

iphone - 显示 UITableView 溢出?

ios - UITableView Section Header 更改当前标题的样式

ios - 如何使用 AVFoundation 框架捕获图像?

ios - 根据日期删除行

ios - 我如何创建两个 UIView 并使用滑动手势将一个覆盖在另一个上?