具有不同格式的UITableView单元格&相同表格部分中的行为

时间:2013-06-28 14:53:05

标签: ios objective-c uitableview

我有一个带有一个部分的UITableView。该一节中的所有单元格都具有从名为PLContactCell的UITableViewCell的子类派生的单元格。

我想做的是,对于表格的最后一行,不要使用PLContactCell。我只是想使用一个普通的UITableViewCell,我可以格式化,但我想。我也希望能够让这个单元不响应被窃听。

我的初始cellForRowAtIndexPath方法是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

PLContactCell *cell = [tableView dequeueReusableCellWithIdentifier:[PLContactCell   reuseIdentifier]];

  if (!cell) {
      cell = [PLContactCell reusableCell];
      cell.delegate = self;
  }

  id modelObject = [[sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

  if ([modelObject isKindOfClass:[NSString class]]) {
    [cell configureWithString:modelObject];
  } else {
      [cell configureWithUser:modelObject];
  }

  return cell;
}

修改 所以我尝试在XIB文件中创建了一个UITableView单元,并向其添加了“newCell”的重用标识符。然后我添加了这段代码:

if (indexPath.row == [[sections objectAtIndex:indexPath.section] count] - 1) {
         NSString *CellIdentifier = @"newCell";
         noFormatCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}

这没有任何作用。我的问题是,我如何访问该部分的最后一行,如何使该单元格不是PLContactCell而是UITableView Cell。

1 个答案:

答案 0 :(得分:0)

如果它总是在最后,您可以考虑使用UITableView的页脚视图。然后,您可以从UITableViewDataSource中保留一些额外的逻辑。

如果它必须作为单元格,则必须在最后一个部分添加额外的行数,然后在-tableView:cellForRowAtIndexPath:实现中进行if语句检查以注意它。我强烈建议你尝试一下页脚方法,因为它更干净,更容易弄清楚你现在几个月/几年后做了什么。

这是一些代码。请注意,如果您在UITableView中使用分组样式,则需要创建另一个部分。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   if (section == sections.count - 1)  //Looking for last section
   {
      return [sections objectAtIndex:section].count + 1; //Add one to the last section
   }

   return [sections objectAtIndex:section].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   NSInteger row = indexPath.row;


   if ((sections.count == indexPath.section) && [sections objectAtIndex:indexPath.section].count == indexPath.row)
   {
       //Here you would create and return your UITableViewCell
   }

PLContactCell *cell = [tableView dequeueReusableCellWithIdentifier:[PLContactCell   reuseIdentifier]];

  if (!cell) {
      cell = [PLContactCell reusableCell];
      cell.delegate = self;
  }

  id modelObject = [[sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

  if ([modelObject isKindOfClass:[NSString class]]) {
    [cell configureWithString:modelObject];
  } else {
      [cell configureWithUser:modelObject];
  }

  return cell;
}