error creating custom tableview separator

后端 未结 1 1542
不知归路
不知归路 2021-01-03 14:28

I\'m trying to create a tableview with custom separator. I made a custom Cell that holds the content, and another that contains only a UIImageView with the separator. The pr

相关标签:
1条回答
  • 2021-01-03 15:19

    try this:

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

    Don't forget to set the Identifier field in IB of the ContentCell.xib to be "ContentCell" and the SeparatorCell.xib to be "SeparatorCell".

    0 讨论(0)
提交回复
热议问题