ios - 在 tableView iOS 中为扩展单元格加载自定义 XIB?

标签 ios objective-c uitableview

我试图在选择时为 UITableViewCell 加载扩展单元格 XIB。我的普通单元格和扩展单元格的内容几乎相同,除了我要填充一些 View 的额外空间在展开的单元格中。现在为了区分普通单元格和展开的单元格,我将展开单元格内的 UILabel 颜色更改为不同的颜色。我可以在单击单元格时加载展开的单元格 XIB < strong>但是问题每个单元格都被展开的单元格XIB 取代。唯一的区别是点击单元格的高度比其他单元格高。当我单击返回展开的单元格时,普通单元格 XIB 会再次加载。如何只为单击的单元格加载展开的单元格 XIB?或者是否有更好的方法来实现这一目标?

下面是我如何扩展单元格。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


if ([self.expandedCells containsObject:indexPath]) {

    [self.expandedCells removeAllObjects];
    isExpanded = NO;
    [self.busTableView beginUpdates];
    [self.busTableView reloadData];
    [self.busTableView endUpdates];

}else{
    isExpanded=YES;

    if (self.expandedCells.count>0) {
        [self.expandedCells removeAllObjects];
    }

    [self.expandedCells addObject:indexPath];
    expCell.expContainer.hidden=YES;

     //Some more code

    [self.busTableView beginUpdates];
    [self.busTableView reloadData];
    [self.busTableView endUpdates];

   }

}

cellForRow 方法如下所示。

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

  if (!isExpanded) {   //BOOL variable set to NO in viewDidLoad initially

    BusListCell *cell =(BusListCell*) [tableView dequeueReusableCellWithIdentifier:nil];
    if (cell==nil) {
    NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"BusListCell" owner:self options:nil];
        cell = nibs[0];

    }
    cell.busName.text = [[busArray objectAtIndex:indexPath.row]valueForKey:@"operatorName"];

    return cell;

 }else{
      expCell = (ExpandedCell*)[tableView dequeueReusableCellWithIdentifier:nil];
      if (expCell==nil) {
         NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ExpandedCell" owner:self options:nil];
        expCell = nibs[0]; 

       }

       expCell.bus_Name.text = [[busArray objectAtIndex:indexPath.row]valueForKey:@"operatorName"];
       return expCell;

      }

    return nil;
  }

最佳答案

而不是检查 cellForRowAtIndexPath:

if (!isExpanded) {}

你必须检查

if (![self.expandedCells containsObject:indexPath]){}

关于ios - 在 tableView iOS 中为扩展单元格加载自定义 XIB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26280049/

相关文章:

objective-c - 测量 cocoa 中的绳子高度

mysql - iOS开发。数据库?数据库?什么是最好的方法?

ios - 动态更新 UITableView 高度和数据

ios - 从 MyTableViewController 中的方法 'viewDidLoad' 获取 MyTableViewCell?

ios - UiTableView 重新加载数据后保留选定的部分/行

ios - 如何获取 uiimage.image 属性更改与实际图像从 Web 出现之间的时间间隔

ios - 滚动后,UICollectionViewCells中的AVPlayers最终会导致应用崩溃

ios - 使用 OpenGL ES 2.0/cocos2d 2.0 绘制圆的最佳方法?

ios - 是否有混合模式或其他我可以为 UIControls 设置的内容,以便我可以看到其下面的图层?

iphone - UIAlertView:单击警报框外部时关闭 UIAlertView