hi i am trying to add uilabel in a group table .The approach i have used is that my whole logic for cell creation is in uitableviewcellforrowatindexpath ie; i have not use seperate class(eg:customcell.h or something like that).
i am succesful in adding the labels BUT CUSTOM CHANGES ARE REFLECTING ONLY TO SECTION ZERO’S ROW.
then i try if(indexpath.section==0) and if(indexpath.section==1) still changes are reflecting only to section zero rows only.Also there is no need of if clause because all the cells look and feel is same.
here is my code:
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
//code STATS here : initializing here for cells background view & selection back ground view
cell.backgroundView =
[[[UIImageView alloc] init] autorelease];
cell.selectedBackgroundView =
[[[UIImageView alloc] init] autorelease];
//code ENDS HERE of initializing
//custom label
UIImage *indicatorImage = [UIImage imageNamed:@"indicator.png"];
cell.accessoryView =
[[[UIImageView alloc]
initWithImage:indicatorImage]
autorelease];
const CGFloat LABEL_HEIGHT = 20;
UIImage *image = [UIImage imageNamed:@"filter.png"];//this image is on top //left of section zero rows
//
// Create the label for the top row
//
topLabel =
[[[UILabel alloc]
initWithFrame:
CGRectMake(
image.size.width + 2.0 * cell.indentationWidth,
0.5 * (aTableView.rowHeight - 2 * LABEL_HEIGHT),
aTableView.bounds.size.width -
image.size.width - 4.0 * cell.indentationWidth
- indicatorImage.size.width,
LABEL_HEIGHT)]
autorelease];
[cell.contentView addSubview:topLabel];
//
// Configure the properties for the text that are the **SAME** on every row
//
topLabel.tag = TOP_LABEL_TAG;
topLabel.backgroundColor = [UIColor clearColor];
topLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
topLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
//testing:
topLabel.text=@"test1";
//
// Create the label for the top row of text
//
bottomLabel =
[[[UILabel alloc]
initWithFrame:
CGRectMake(
image.size.width + 2.0 * cell.indentationWidth,
0.5 * (aTableView.rowHeight - 2 * LABEL_HEIGHT) + LABEL_HEIGHT,
aTableView.bounds.size.width -
image.size.width - 4.0 * cell.indentationWidth
- indicatorImage.size.width,
LABEL_HEIGHT)]
autorelease];
[cell.contentView addSubview:bottomLabel];
//
// Configure the properties for the text that are the same on every row
//
bottomLabel.tag = BOTTOM_LABEL_TAG;
bottomLabel.backgroundColor = [UIColor clearColor];
bottomLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
bottomLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
bottomLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize] - 2];
bottomLabel.text=@"test2";
}
//testing for section 2
//this image is on top left of section one rows
UIImage *image1 = [UIImage imageNamed:@"akshardham.jpg"];
topLabel1 =
[[[UILabel alloc]
initWithFrame:
CGRectMake(
image1.size.width + 2.0 * cell.indentationWidth,
0.5 * (aTableView.rowHeight - 2 * LABEL_HEIGHT),
aTableView.bounds.size.width -
image1.size.width - 4.0 * cell.indentationWidth
- indicatorImage.size.width,
LABEL_HEIGHT)]
autorelease];
[cell.contentView addSubview:topLabel1];
topLabel1.tag = TOP_LABEL_TAG1;
topLabel1.backgroundColor = [UIColor clearColor];
topLabel1.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
topLabel1.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
topLabel1.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
//testing:
topLabel1.text=@"test2section";
}
}
else
{
topLabel = (UILabel *)[cell viewWithTag:TOP_LABEL_TAG];
bottomLabel = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG];
topLabel1 = (UILabel *)[cell viewWithTag:TOP_LABEL_TAG1];
}
any suggestion?thanks..
it’s like your whole code is in
So you won’t go in when you will scroll because you will have enough cells wich are created. You will reuse them and the cell won’t be nil and you won’t go in your customization code.
Do: