iosuitableviewtextlabel

Adding two text labels (one dynamic and one static) in the same table cell


I have a tableview with custom cells inside a view controller. My tableview works properly. What I am trying to do is develop the image below programmatically. Where "label" is the text that is custom and changes depending on some input. How can I include these 2 labels (in cellForRowAtIndexPath:) and determine their position in the cell. Image contains 2 different table cells.

Image contains 2 different table cells

I am aware how to do it through storyboard, but I need to do it programmatically cause I am using dynamic cells in xCode 4.5.

The image refers to the index cells 1 and 2. I have only managed to include one text label per cell till now.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];
}




    switch ([indexPath row])
    {
        case 0:
        {
           // image cell - image resize and centred


            UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(30,2, 180, 180)];
            imv.image=[UIImage imageNamed:@"test1.jpg"];


            imv.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
            [cell.contentView addSubview:imv];

            imv.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);

            cell.accessoryType = UITableViewCellAccessoryNone;

            break;
        }
        case 1:
        {


            cell.textLabel.text = @"Name";


            break;
        }

        case 2:
        {
            cell.textLabel.text = @"Manufacturer";

            break;
        }

        case 3:
        {
            cell.textLabel.text = @"Overall Score";

            break;
        }

        case 4:
        {
            cell.textLabel.text = @"Description";
            cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

            break;
        }

        case 5:
        {
            cell.textLabel.text = @"Videos";
            cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

            break;
        }
            }


    return cell;

}

Thank you in advance


Solution

  • Check the default UITableViewCell styles first: UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle.

    If you can use them, you don't need to subclass UITableViewCell. Otherwise you're going to have to do just that and put 3 properties down: a UIView and 2 UILabel's. The reason is if you don't use default cell styles, you cannot move or add cell elements.

    Your UITableViewCell subclass should have the following code:

    @interface UITableViewCellSubClass : UITableViewCell
    @property (nonatomic, strong) UIView *view;
    @property (nonatomic, strong) UILabel *label1;
    @property (nonatomic, strong) UILabel *label2;
    @end
    
    @implementation UITableViewCellSubClass
    @synthesize view;
    @synthesize label1;
    @synthesize label2;
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        view = [[UIView alloc] initWithFrame:self.frame];
        [self addSubview:view];
        // initiate label1 with position (10,10,150,20)
        label1 = [[UILabel alloc] initWithFrame:CGRectMake(10,10,150,20)];
        // initiate label2 with position (170,10,150,20)
        label2 = [[UILabel alloc] initWithFrame:CGRectMake(170,10,150,20)];
        [view addSubview:label1];
        [view addSubview:label2];
    }
    return self;
    }
    @end
    

    Then you can return that in your cellForRowAtIndex: method and basically:

    UITableViewCellSubclass *cell = [[UITableViewCellSubclass alloc] initWithStyle:UITableViewCellDefault reuseIdentifier:@"cell"];
    [cell.label1 setText:@"text"];
    [cell.label2 setText:@"more text"];
    

    Hope this helps and don't forget to #import "UITableViewCellSubClass.h"