iosuitableview

How to view custom UITableViewCell in iOS


I am trying to display results in a UITableView in my iOS application using a custom UITableViewCell. I am able to view the contents of my Array if I use the cell's textLabel, and detailTextLabel properties, but if I try to link the UITableView with the custom cell class to display the contents of the Array, I only get the default values from Interface Builder displaying in my table.

This is the relevant code that I have:

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

The method below is what I have in my CustomTableCell class:

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

Where _transaction, _date, and _type are labels. Here is a screenshot of my Interface Builder inside MainStoryboard:

enter image description here

I have tried changing the style from "Basic", to "Custom", to "Subtitle", but that does not help. It only displays the default values of the table, and those settings change the way they appear, but at no point are the contents of my Array showing up in the table. What am I doing wrong?


Solution

  • You don't show the 3 objects in the Master View cell. Change the 'Style' from Basic to Custom and drop in your objects.

    In the 'Identity Inspector' make sure to rename the Class to 'CustomTableCell'.

    Do the views (transaction, date & type) have properties in the CustomTableCell.h and are they connected to the 'CustomTableCell' in IB?

    Here's an image to point out the last two:

    enter image description here