Tutorial :Multiple labels in a table cell view



Question:

I want to add three labels into a cell dynamically, cell is created dynamically as well, but I don't know how to create labels and add it into the cell in Objective-C (iPhone). Can anybody help me?


Solution:1

By "dynamically" I assume you mean via code, not via Interface Builder. I also assume you know how to use -tableView:cellForRowAtIndexPath:.

Then things are simple. The UITableViewCell has a readonly contentView property. You can just add your three UILabel's as a subview of contentView.

UIView* view = cell.contentView;  UILabel* label1 = [[UILabel alloc] initWithFrame:…];  …  [view addSubview:label1];  [label1 release];  …  


Solution:2

You should be careful about reuseidentifier.

when you want to keep your TableView scrolled smoothly.

You should use reuseidentifier.however, if you add three labels every time.

the cell would became mass.

0.get Cell by reuseidentifier,get the label by tag if the label has exsited.

if(label exist)

{

remove the label or reuse it.

}

else

{

1.init an UILabel.

2.set Tag.

3.add to contentView.

4.use the label.

}


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »