error creating custom tableview separator

旧城冷巷雨未停 提交于 2019-11-30 16:20:36

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".

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