Search code examples
objective-cioscocoa-touchuitableviewuilabel

How can i put label into the table cell?


table cell

this is the cell i want to make the left side is cell.text and the right part is label. Now the table style is

UITableViewStyleGrouped When i try to make the label i write these codes.

cell.textLabel.text = @"All";
UIView* view = cell.contentView;
UILabel* label1 = [[UILabel alloc] initWithFrame:cell.frame];
label1.textColor = [UIColor blackColor];
label1.textAlignment = UITextAlignmentCenter;
label1.text = @"%15";
[view addSubview:label1];
[label1 release];

But this doesnt work because cell of the label cover one and another.Can any one help me to make this kind of look with code of course.


Solution

  • The problem in your code seems to be the label1's frame. Change the its frame like the following.

    CGRect lFrame = CGRectMake(cell.frame.width - 100, 0, 100, cell.frame.height);
    UILabel* label1 = [[UILabel alloc] initWithFrame:lFrame];
    

    Using exisiting style: The style you are using is already predefined. No need to add your custom label to the cell. You can achieve this style by specifying table cell's style to UITableViewCellStyleValue1.

    [UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 ....
    

    You can change the font properties like style, color and size of the cell's textLabel and detailedTextLabel to fit your needs.