UITableView with two custom cells (multiple identifiers)

匿名 (未验证) 提交于 2019-12-03 02:27:02

问题:

I am trying to put a cell as a space between each cell - which will be hidden by setting alpha = 0. In my table, the space cells will be for rows that are odd.

Note that the actual cell height is 85, but the hidden cell height (ie space between cells) is 20.

The problem is that the space cell height is 85, but not 20, I don't know why. Maybe the cell is not loaded correctly.

Cell here is the UITableViewCell - the actual cell - with identifier 'Cell'.

Cell2 is the space with identifier 'Space'.

Each class above has its own UITableViewCell class and the XIB files are also assigned to each of them. The identifier is also set in the IB for each Xib.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier1 = @"Cell"; static NSString *CellIdentifier2 = @"Space";  Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];  if(!cell) {     NSArray *ar = [[NSBundle mainBundle] loadNibNamed:@"CellView" owner:nil options:nil];     for (id obj in ar)     {         if ([obj isKindOfClass:[Cell class]])         {             cell = (Cell *)obj;             break;         }     } }  if (indexPath.row % 2 == 1) {     Cell2 *cell2 = (Cell2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];      if (!cell2)     {         NSArray *ar = [[NSBundle mainBundle] loadNibNamed:@"Cell2" owner:nil options:nil];         for(id obj in ar)         {             if([obj isKindOfClass:[Cell2 class]])             {                 cell2 = (Cell2 *)obj;                 break;             }         }          // Method 1         cell2 = [[Cell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];          // Method 2         //cell2 = [[Cell2 alloc] init];         // Method 3         //cell2 = (Cell2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];          [cell2.contentView setAlpha:0];         // prevent selection and other stuff         [cell2 setUserInteractionEnabled:NO];     }     return cell2; } else {     // Configure the actual cell }   return cell;

}

回答1:

* I've renamed some of your NIB/Class names for a better understanding. *

First, you should register each cells' NIB:

- (void)viewDidLoad{     [super viewDidLoad];      static NSString *CellIdentifier1 = @"ContentCell";     static NSString *CellIdentifier2 = @"SpaceCell";      UINib *nib = [UINib nibWithNibName:@"CellViewNIBName" bundle:nil];     [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier1];      nib = [UINib nibWithNibName:@"CellSpaceNIBName" bundle:nil];     [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier2];      self.contentView.hidden = YES;     [self loadData]; }

Because you have the NIBs registered, dequeueReusableCellWithIdentifier: will always return a cell:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath        
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!