Error creating custom table separator

I am trying to create a tableview with a custom separator. I created a custom cell containing the content and another that contains only the UIImageView with a separator. The problem is how the content is accessed. I can not use indexPath.row

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier: @"CustomCell"]; if(cell == nil) { cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil] objectAtIndex:0]; } SeparatorCell *cellSeparator = (SeparatorCell *) [tableView dequeueReusableCellWithIdentifier: @"SeparatorCell"]; if(cellSeparator == nil) { cellSeparator = [[[NSBundle mainBundle] loadNibNamed:@"SeparatorCell" owner:self options:nil] objectAtIndex:0]; } if(indexPath.row % 2) { UIFont * myCustomFont = [UIFont fontWithName:@"Arial Black" size:16]; [cell.titulo setFont:myCustomFont]; cell.titulo.textColor = [UIColor colorWithRed:103/255.0f green:169/255.0f blue:0/255.0f alpha:1]; cell.titulo.text = [Title objectAtIndex:myRow]; cell.subtitle.text = [Subtitle objectAtIndex:myRow]; cell.tag = myRow; myRow++; } else [cellSeparator.imagem setImage:[UIImage imageNamed:@"CustomSeparator.png"]]; if(indexPath.row % 2) return cell; else return cellSeparator; } 

myRow

- global NSInteger.

I have already tried

 if(myRow == Title.count) myRow=0; else myRow++; 
+2
objective-c uitableview custom-cell
Jul 12 2018-12-12T00:
source share
1 answer

try the following:

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. NSInteger numOfRowsIncludeSeparator = 0; if (self.model.count > 0) { NSInteger numOfSeperators = self.model.count - 1; numOfRowsIncludeSeparator = self.model.count + numOfSeperators; } return numOfRowsIncludeSeparator; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ContentCellIdentifier = @"ContentCell"; static NSString *SeparatorCellIdentifier = @"SeparatorCell"; UITableViewCell *cell = nil; if (indexPath.row % 2 == 0) { // this is a content cell cell = [tableView dequeueReusableCellWithIdentifier:ContentCellIdentifier]; if(cell == nil) { cell = [[[NSBundle mainBundle] loadNibNamed:@"ContentCell" owner:self options:nil] objectAtIndex:0]; } // get the model index NSInteger indexInModel = indexPath.row / 2; // get the model for this row NSString *modelObject = [self.model objectAtIndex:indexInModel]; // Configure the cell... cell.textLabel.text = modelObject; } else { // this is a separator cell cell = [tableView dequeueReusableCellWithIdentifier:SeparatorCellIdentifier]; if(cell == nil) { cell = [[[NSBundle mainBundle] loadNibNamed:@"SeparatorCell" owner:self options:nil] objectAtIndex:0]; } } return cell; } 

Remember to set the Identifier field in IB ContentCell.xib to "ContentCell" and SeparatorCell.xib to "SeparatorCell".

+8
Jul 12 2018-12-12T00:
source share



All Articles