Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView with two custom cells (multiple identifiers)

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; 

}

like image 538
MohamMad Salah Avatar asked Jan 13 '13 13:01

MohamMad Salah


People also ask

How can we use a reusable cell in UITableView?

For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.


1 Answers

* 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      *)indexPath {     static NSString *CellIdentifier1 = @"ContentCell";     static NSString *CellIdentifier2 = @"SpaceCell";      // Space Cell     if (indexPath.row % 2 == 1) {         CellSpace *cell = (CellSpace *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];         return cell;     }      // Content cell     else {         CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];         // Configure cell         return cell;     } } 

Last, but not least, make sure to implement the following delegate method:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {     // Space cell's height     if (indexPath.row % 2 == 1) {         return 20.0f;     }      // Content cell's height     else {         return 80.0f;     } } 
like image 105
Attila H Avatar answered Oct 05 '22 05:10

Attila H