簡體   English   中英

根據部分填充tableView的每一行

[英]Populate each row of a tableView according to a section

根據外部數據,我有一個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;
}

問題是如何正確填充行? 我越來越糟糕的功能是,如果使用數組中的值,那么該值或其他數組的相等值將不會顯示在單元格中。

使用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方法中使用數據源,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM