objective-c - 创建自定义表格 View 分隔符时出错

标签 objective-c uitableview custom-cell

我正在尝试创建一个带有自定义分隔符的表格 View 。我制作了一个包含内容的自定义 Cell,另一个仅包含带分隔符的 UIImageView。 问题在于如何访问内容。 我不能使用 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

是一个全局的 NSInteger。

我已经试过了

if(myRow == Title.count) myRow=0;
        else myRow++;

最佳答案

试试这个:

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

不要忘记将 ContentCell.xib 的 IB 中的 Identifier 字段设置为“ContentCell”,将 SeparatorCell.xib 设置为“SeparatorCell”。

关于objective-c - 创建自定义表格 View 分隔符时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11453366/

相关文章:

ios - 如何根据在 Table View Controller Popup 上所做的选择更新父 View

iphone - Objective-C 在 iPhone 上测试应用程序,更新数据,数据没有改变

ios - 如何更改段分隔符颜色?

ios - 无法更改所选自定义单元格中的图像

objective-c - iOS 自定义单元格可扩展 - 索引 0 问题

ios - 在 UITableView 中滚动时如何隐藏/显示图像?

ios - 将 NSArray 中的 TableView 项目分组到 Swift 中的 JSON 响应中返回

ios - 使用xib文件的自定义表格 View 的单元格,无法单击/选择/点击

ios - 如何使用 UITableViewAutomaticDimension 更新单元格的高度?

IOS/objective-C : Detect Button Press in Custom Tableview Cell?