I want to add three labels into a cell dynamically,开发者_StackOverflow中文版 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?
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];
…
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.
}
精彩评论