如何在iOS中查看自定义UITableViewCell

问题描述 投票:0回答:1

我正在尝试使用自定义 UITableViewCell 在我的 iOS 应用程序中的 UITableView 中显示结果。如果我使用单元格的textLabel和detailTextLabel属性,我可以查看数组的内容,但是如果我尝试将UITableView与自定义单元格类链接以显示数组的内容,我只能从接口获取默认值生成器显示在我的表格中。

这是我拥有的相关代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
    }
    
    // Configure the cell...
    
    
    TransactionList *tList = [_list objectAtIndex:indexPath.row];
/*    
    cell.transaction.text = tList.transaction;
    cell.type.text = tList.type;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"ddMMYYYY"];
    
    NSString *dateAsString = [formatter stringFromDate:tList.date];
    cell.date.text = dateAsString;

  */

//this code below displays the contents just fine, but the block of code above does not.    

    cell.textLabel.text = tList.transaction;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@, %@", tList.type, tList.status];
    
    return cell;
}

下面的方法是我的 CustomTableCell 类中的方法:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.contentView.frame = CGRectMake(0, 0, 320, 80);
        
        _transaction = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 80, 40)];
        [_transaction setBackgroundColor:[UIColor clearColor]];
        [_transaction setFont:[UIFont systemFontOfSize:12]];
        [_transaction setNumberOfLines:2];
        [_transaction setLineBreakMode:NO];
        [self.contentView addSubview:_transaction];
        
        _date = [[UILabel alloc] initWithFrame:CGRectMake(100, 5, 80, 40)];
        [_date setBackgroundColor:[UIColor clearColor]];
        [_date setFont:[UIFont systemFontOfSize:12]];
        [_date setNumberOfLines:2];
        [_date setLineBreakMode:NO];
        [self.contentView addSubview:_date];
        
        _type = [[UILabel alloc] initWithFrame:CGRectMake(200, 5, 80, 40)];
        [_type setBackgroundColor:[UIColor clearColor]];
        [_type setFont:[UIFont systemFontOfSize:12]];
        [_type setNumberOfLines:2];
        [_type setLineBreakMode:NO];
        _type.lineBreakMode = NO;
        [self.contentView addSubview:_type];
        
        
    }
    return self;
}

其中 _transaction、_date 和 _type 是标签。这是我的 Interface Builder 在 MainStoryboard 中的屏幕截图:

enter image description here

我尝试将样式从“基本”更改为“自定义”,再更改为“字幕”,但这没有帮助。它仅显示表的默认值,并且这些设置会更改它们的显示方式,但我的数组的内容绝不会显示在表中。我做错了什么?

ios uitableview
1个回答
0
投票

您没有在主视图单元格中显示 3 个对象。将“样式”从基本更改为自定义,然后放入您的对象。

在“身份检查器”中,确保将类重命名为“CustomTableCell”。

视图(交易、日期和类型)在 CustomTableCell.h 中是否具有属性,并且它们是否连接到 IB 中的“CustomTableCell”?

这是指出最后两个的图像:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.