hi guys i have a normal UITableViewController with some cell inside,
i have just 1 cell that has this property:
cell.backgroundColor = [UIColor lightGrayColor];
it show me a gray color on a cell (good).
The problem comes when i use the scroller, (i tap on the window and i go down for see
other cells) in this case the Gray color of the cell move (insanely) from the Position where it was
to another cell.
why ??
the code:
static NSString *CellIdentifier = @”Cell”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(cell.backgroundColor == [UIColor lightGrayColor])
{
cell.backgroundColor = [UIColor lightGrayColor];
}
else
{
cell.backgroundColor = [UIColor clearColor];
}
switch (currentStatus) {
case KBI_NAME:
{
switch (indexPath.section) {
case 0:
{
if(indexPath.row == 0)
{
if (currentUser.currentKBI != nil && ![currentUser.currentKBI isEqualToString:@""]) {
cell.textLabel.text = currentUser.currentKBI;
}
else{
cell.textLabel.text = @"asdf";
}
cell.userInteractionEnabled = NO;
cell.backgroundColor = [UIColor lightGrayColor];
}
if(indexPath.row == 1)
{
cell.textLabel.text = @"xyz";
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
break;
}
case 1:
When you scroll in a
UITableView, cells that are scrolled out of view are reused for cells that scroll into view. That’s what the following line is for:So if this line returns a cell, you need to set the background color every time because you might have gotten the cell with the gray background.
Update
Your code for setting the background color doesn’t make sense. If the recycled cell has a gray background, you set it to gray again even if you need a different color. Those like should look something like this:
Update 2
It’s probably much easier, if you simply initialize the cell every time:
If you need a gray background, the color is later changed again to gray.