Reputation: 1681
In my app i am adding 3 UILabel to each Table view cell in the following way.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ShowMoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [self getCellContentView:CellIdentifier];
return cell;
}
- (UITableViewCell *)getCellContentView:(NSString *)cellIdentifier
{
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier] autorelease];
cell.backgroundColor=[UIColor clearColor];
for(int j = 0; j < 3; j++)
[cell insertSubview:[items objectAtIndex:j] atIndex:j];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Here items is an NSMutableArray
and it has got elements of UILabel. I am trying to add three labels
in each cell
. But it is displaying the 3 labels
only in last row.
Am i doing something wrong here ?
Thanks
Upvotes: 0
Views: 184
Reputation: 173
UILabel *lblAdd=[[UILabel alloc]initWithFrame:CGRectMake(10, 60, 150, 20)];
lblAdd.backgroundColor =[UIColor clearColor];
lblAdd.textColor=[UIColor blackColor];
lblAdd.text=@"Label";
lblAdd.font=[UIFont fontWithName:@"Helvetica" size:12];
[cell.contentView addSubview:lblAdd];
That's it you will find labels in every row Happy Coding
Upvotes: 0
Reputation: 2664
You are trying to add the same UILabel
objects to different cells. UIViews
(which UITableViewCell
is a subclass of) can have only one parent view. You should create 3 new labels for every cell (if it doesn't contain them yet).
Upvotes: 3
Reputation: 1692
The view objects eg labels are not supposed to be stored in any form. Best way to approach this problem would be to store the data items in the array and use cells to display those only.
Create a custom Cell say
`
MyTableViewCell : UITableViewCell
@property(nonatomic,strong)UILabel * lbl1;
@property(nonatomic,strong)UILabel * lbl2;
@property(nonatomic,strong)UILabel * lbl3;
@end;
Then set the values to these labels using like
cell.lbl1.text = @" some text";
cell.lbl2.text = @" some text";
cell.lbl3.text = @" some text";
Upvotes: 0