【问题标题】:error creating custom tableview separator创建自定义表格视图分隔符时出错
【发布时间】:2012-07-12 13:53:20
【问题描述】:

我正在尝试创建一个带有自定义分隔符的表格视图。我制作了一个包含内容的自定义单元格,另一个只包含一个带有分隔符的 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;
}

我的行

是一个全局 NSInteger。

我已经试过了

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

【问题讨论】:

  • 使用时会发生什么?
  • 我在 tableview 中下去,一行消失了,当我再次返回查看该行时,Xcode 给我一个错误,试图访问数组中不存在的对象,我相信变量 >myRow 没有被重置。
  • 不使用变量myRow,为什么不直接使用indexPath.row%2
  • indexPath.row 单独是不够的,正如@Eyal 所说的答案是 indexPath.row/2。
  • 哎呀,这就是我的意思

标签: objective-c uitableview custom-cell


【解决方案1】:

试试这个:

- (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”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    相关资源
    最近更新 更多