简体   繁体   English

为tableView iOS中的扩展单元格加载自定义XIB吗?

[英]Load a custom XIB for expanded cell in tableView iOS?

I am trying to load a expanded cell XIB for a UITableViewCell when it is selected.The content of my normal cell and expanded cell is almost same except for the extra space where I would populate some views in expanded cell.Now to differentiate between the normal cell and expanded cell I have changed the UILabel colors inside the expanded cells to a different color.I am able to load the expanded cell XIB on clicking a cell however the problem is that every cell is getting replaced by expanded cell XIB .The only difference is that the height of clicked cell is more than others .And when I click back the expanded cell, Normal cell XIB 's are loaded again.How can I load the expanded cell XIB only for the clicked cell? 我正在尝试为UITableViewCell选中时加载一个扩展单元格XIB。我的常规单元格和扩展单元格的内容几乎相同,除了我将在扩展单元格中填充一些视图的额外空间之外。单元格和扩展单元格我已将扩展单元格内的UILabel颜色更改为其他颜色。单击单元格时我能够加载扩展单元格XIB, 但是 问题每个单元格都被扩展单元格XIB取代 。唯一的区别是是单击的单元格的高度是否比其他单元格高。当我单击扩展的单元格时,将再次加载普通单元格XIB 。如何仅为单击的单元格加载扩展的单元格XIB Or is there a better way to achieve this thing? 还是有更好的方法来实现这一目标?

Here's how I am expanding the cell. 这就是我扩展单元格的方式。

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

   }

}

And the cellForRow method looks like this. 而且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;
  }

Instead of checking in cellForRowAtIndexPath: 而不是检查cellForRowAtIndexPath:

if (!isExpanded) {}

you have to check 你必须检查

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM