Reputation: 303
I have a UITable that can collapse on header section click. I need the header sections to have unique text in them.
I can use cell.textlabel.text = @""; and this works fine, however I want to add more labels to the cell and position these i.e
lbl123 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 20)];
lbl123.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
lbl123.textColor = [UIColor blackColor];
lbl123.backgroundColor = [UIColor clearColor];
lbl123.highlightedTextColor = [UIColor whiteColor];
lbl123.opaque = NO;
lbl123.adjustsFontSizeToFitWidth = TRUE;
lbl123.numberOfLines = 0;
lbl123.lineBreakMode = UILineBreakModeWordWrap;
[cell addSubview:lbl123];
lbl123.text = @"test";
but when I add this to the section on "cellforindexpath" It replicates itself on cells further down the table.
any ideas why this is happening??
full code below:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellValue = 0;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"MyIdentifier"];
}
if (tableView.tag == 2)
{
UILabel *lbl123;
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
cell.textLabel.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
if (!indexPath.row)
{
cell.textLabel.backgroundColor = [UIColor clearColor];
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
[bgView setBackgroundColor:[UIColor blackColor]];
[bgView setAlpha:0.3];
[cell setBackgroundView:bgView];
lbl123 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 20)];
lbl123.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
lbl123.textColor = [UIColor blackColor];
lbl123.backgroundColor = [UIColor clearColor];
lbl123.highlightedTextColor = [UIColor whiteColor];
lbl123.opaque = NO;
lbl123.adjustsFontSizeToFitWidth = TRUE;
lbl123.numberOfLines = 0;
lbl123.lineBreakMode = UILineBreakModeWordWrap;
[cell addSubview:lbl123];
lbl123.text = @"";
if(indexPath.section == 1)
{
cell.textLabel.font = [UIFont fontWithName:@"Trebuchet MS-Bold" size:15.0f];
cell.textLabel.text = [NSString stringWithFormat:@"My Results Overview:"];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.numberOfLines = 3;
cell.textLabel.opaque = YES;
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 294, 80)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = [UIImage imageNamed:@"Blue bar 720x115.png"];
cell.backgroundView = av;
}
if(indexPath.section == 2)
{
cell.textLabel.font = [UIFont fontWithName:@"Trebuchet MS-Bold" size:15.0f];
cell.textLabel.text = [NSString stringWithFormat:@"Result1:"];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.numberOfLines = 3;
cell.textLabel.opaque = YES;
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 294, 120)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
if ([confidenceCOL isEqualToString:@"Red"])
{
av.image = [UIImage imageNamed:@"redPanel.png"];
}
if ([confidenceCOL isEqualToString:@"Green"])
{
av.image = [UIImage imageNamed:@"greenPanel.png"];
}
if ([confidenceCOL isEqualToString:@"Purple"])
{
av.image = [UIImage imageNamed:@"purplePanel.png"];
}
cell.backgroundView = av;
lbl123.text = @"Test";
}
if(indexPath.section == 3)
{
cell.textLabel.font = [UIFont fontWithName:@"Trebuchet MS-Bold" size:15.0f];
cell.textLabel.text = [NSString stringWithFormat:@"Result2:"];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.numberOfLines = 3;
cell.textLabel.opaque = YES;
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 294, 120)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
if ([ambitionCOL isEqualToString:@"Red"])
{
av.image = [UIImage imageNamed:@"redPanel.png"];
}
if ([ambitionCOL isEqualToString:@"Green"])
{
av.image = [UIImage imageNamed:@"greenPanel.png"];
}
if ([ambitionCOL isEqualToString:@"Purple"])
{
av.image = [UIImage imageNamed:@"purplePanel.png"];
}
cell.backgroundView = av;
lbl123.text = @"TestING!";
}
if ([expandedSections containsIndex:indexPath.section]){}
else{}
}
else
{
if(indexPath.section == 2)
{
cellValue = Result1Moretext;
}
if(indexPath.section == 3)
{
cellValue = Result2Moretext;
}
static NSUInteger const k1 = 1;
// Declare references to the subviews which will display the data.
UILabel *lbl1 = nil;
static NSString *kCellID = @"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 300)];
lbl1.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
lbl1.tag = k1;
lbl1.textColor = [UIColor blackColor];
lbl1.backgroundColor = [UIColor clearColor];
lbl1.highlightedTextColor = [UIColor whiteColor];
lbl1.opaque = NO;
lbl1.adjustsFontSizeToFitWidth = TRUE;
lbl1.numberOfLines = 0;
lbl1.lineBreakMode = UILineBreakModeWordWrap;
[cell.contentView addSubview:lbl1];
}else {
lbl1 = (UILabel *)[cell.contentView viewWithTag:k1];
}
cell.accessoryView.tag = indexPath.row;
lbl1.text = [NSString stringWithFormat:@"%@", cellValue];
return cell;
}
}
else
{
}
}
Thanks
Upvotes: 0
Views: 391
Reputation: 26
Don't forget that cells are reused in UITableViews to keep memory usage low. So what's happening is you are making a cell, and adding the label to it, then after that cell scrolls of the screen it goes into a reuse pool, gets re-added as a new cell further down and still has the label in it.
What you should do is create all labels etc only when you make a new cell (in the cell==nil section, that's where a cell was not dequeued), and set all the labels in the rest of the code.
You can use view tags to find the label views again later.
eg.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"MyIdentifier"];
lbl123 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 20)];
lbl123.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
lbl123.textColor = [UIColor blackColor];
lbl123.backgroundColor = [UIColor clearColor];
lbl123.highlightedTextColor = [UIColor whiteColor];
lbl123.opaque = NO;
lbl123.adjustsFontSizeToFitWidth = TRUE;
lbl123.numberOfLines = 0;
lbl123.lineBreakMode = UILineBreakModeWordWrap;
[cell addSubview:lbl123];
lbl123.text = @"";
lbl123.tag=123;
} else {
lbl123 = (UILabel *)[cell viewWithTag:123];
}
if(indexPath.section == 2)
{
lbl123.text = @"Test";
}
Upvotes: 1