根据一个部分填充tableView的每一行

时间:2013-11-29 04:45:00

标签: ios objective-c tableview

根据外部数据,我有一个tableView女巫有部分和可变行数。

对于cellForRowAtIndexPath中的填充单元格,我有一个包含其他数组的数组:

(
    (
    "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"
)
)

每个数组的objectAtIndex:0等于部分标题,并且必须放在该部分中。

我的代码是这样的:

- (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;
}

问题是如何正确填充行?我的功能很糟糕,如果使用数组中的值,那么该单元格或其他数组的相等值就不会显示在单元格中。

1 个答案:

答案 0 :(得分:0)

使用dataSource结构,很难像这样直接填充tableView。如果可以,重新设计您的dataSource结构。如果从任何服务或类似项返回,则在填充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 cellForRowAtIndexPath:(NSIndexPath*) indexPath方法

中直接使用dataSource
- (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];
}