Ios 根据节填充tableView的每一行

Ios 根据节填充tableView的每一行,ios,objective-c,tableview,Ios,Objective C,Tableview,我有一个tableView,根据外部数据,它具有部分和可变行数 对于cellforrowatinexpath中的填充单元格,我有一个数组,其中包含以下类似的其他数组: ( ( "segunda-feira, 28 de janeiro 2013", 1978, "Luana de Lima ", "09:00" ), ( "s\U00e1bado, 26 de janeiro 2013", 1979, "Marcus Vin

我有一个tableView,根据外部数据,它具有部分和可变行数

对于
cellforrowatinexpath
中的填充单元格,我有一个数组,其中包含以下类似的其他数组:

(
    (
    "segunda-feira, 28 de janeiro 2013",
    1978,
    "Luana de Lima ",
    "09:00"
),
    (
    "s\U00e1bado, 26 de janeiro 2013",
    1979,
    "Marcus Vinicius Gimenes",
    "12:00"
),
    (
    "s\U00e1bado, 26 de janeiro 2013",
    1979,
    "Joana dark",
    "12:30"
),
    (
    "s\U00e1bado, 26 de janeiro 2013",
    1978,
    "Joana dark",
    "09:00"
),
    (
    "ter\U00e7a-feira, 29 de janeiro 2013",
    1978,
    "Joana dark",
    "09:00"
),
    (
    "domingo, 27 de janeiro 2013",
    1978,
    "Marcus Vinicius Gimenes",
    "09:00"
)
)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =nil;
    static NSString *identifier = @"ScheduleCell";
    ScheduleCell *cell = (ScheduleCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ScheduleCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    if([[self tableView:tableView titleForHeaderInSection:indexPath.section] isEqualToString:    [[self.appointmentsFormatted objectAtIndex:indexPath.row] objectAtIndex:0]])
    {
        cell.nome.text = [[self.appointmentsFormatted objectAtIndex:indexPath.row] objectAtIndex:2];
        cell.horario.text = [[self.appointmentsFormatted objectAtIndex:indexPath.row] objectAtIndex:3];               
    }
    return cell;
}
其中,每个数组的
objectAtIndex:0
等于一个节标题,必须放在该节中

我的代码如下:

(
    (
    "segunda-feira, 28 de janeiro 2013",
    1978,
    "Luana de Lima ",
    "09:00"
),
    (
    "s\U00e1bado, 26 de janeiro 2013",
    1979,
    "Marcus Vinicius Gimenes",
    "12:00"
),
    (
    "s\U00e1bado, 26 de janeiro 2013",
    1979,
    "Joana dark",
    "12:30"
),
    (
    "s\U00e1bado, 26 de janeiro 2013",
    1978,
    "Joana dark",
    "09:00"
),
    (
    "ter\U00e7a-feira, 29 de janeiro 2013",
    1978,
    "Joana dark",
    "09:00"
),
    (
    "domingo, 27 de janeiro 2013",
    1978,
    "Marcus Vinicius Gimenes",
    "09:00"
)
)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =nil;
    static NSString *identifier = @"ScheduleCell";
    ScheduleCell *cell = (ScheduleCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ScheduleCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    if([[self tableView:tableView titleForHeaderInSection:indexPath.section] isEqualToString:    [[self.appointmentsFormatted objectAtIndex:indexPath.row] objectAtIndex:0]])
    {
        cell.nome.text = [[self.appointmentsFormatted objectAtIndex:indexPath.row] objectAtIndex:2];
        cell.horario.text = [[self.appointmentsFormatted objectAtIndex:indexPath.row] objectAtIndex:3];               
    }
    return cell;
}

问题是如何右键填充行?我得到了一个糟糕的函数,如果使用数组中的一个值,那么该数组或其他数组的相等值就不会显示在单元格中。

使用数据源结构,很难像这样直接填充tableView。如果可以,请重新设计数据源结构。如果它是从任何服务或类似服务返回的,请在填充tableView之前更改结构

- (NSMutableArray *)createDataSource:(NSArray *)inputArray
{
  NSMutableArray *outputArray = [[NSMutableArray alloc] init];
  NSMutableArray *titleArray = [[NSMutableArray alloc] init];

  for (NSArray *rowArray in inputArray) {
    if (![titleArray containsObject:[rowArray objectAtIndex:0]]) {
        [titleArray addObject:[rowArray objectAtIndex:0]];
    }
  }

  for (NSString *title in titleArray) {
    NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        return [[evaluatedObject objectAtIndex:0] isEqualToString:title];
    }];
    NSArray *sectionArray = [inputArray filteredArrayUsingPredicate:predicate];
    [outputArray addObject:sectionArray];
  }

  return outputArray;
}
之后,您可以直接在
-(UITableViewCell*)tableView:(UITableView*)tableView cellforrowatinexpath:(nsindepath*)indepath
方法中使用数据源

- (UITableViewCell*) tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath
{
  // I am using outputArray for your self.appointmentsFormatted which is generated from the method

  cell.nome.text = [[[outputArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectAtIndex:2];
}

检查我的answe,它可能会解决您的崩溃问题。为什么要这样使用数据源结构?在填充tableView之前,您可以为每个部分保留单独的数组。您的数据非常糟糕。我想不出一个简单的解决方案来填充表格单元格。我建议重新对它们进行一些分析,使它们对开发人员更加友好。谢谢,使用您将我的数组作为参数传递的方法,我如何访问in-cellForRowAIndexPath?比如:cell.nome.text=@“数组中的字符串”@ArthurMastropietro您可以访问数组中的字符串。我已经更新了代码。试试看..不错,但是当我尝试使用这种语法时:cell.nome.text=[[[self.appointmentsformated objectAtIndex:indexath.section]indexPath.row]objectAtIndex:2];它给了我一个错误:预期为']'@ArthurMastropietro抱歉。。缺少一个
objectAtIndex
。。我已经改正了。。现在试试..我试过了,但是我得到了objectAtIndex.indexPath.row的一个超出边界的数组:“索引1超出边界[0..0]”。我认为从方法返回的数据有一个索引为1(0)的数组。是吗?