简体   繁体   中英

If statements are showing weird results in a tableView

In my cellForRowAtIndexPath method I am using following if statements:

if ([taskitem.isCritical isEqualToString:@"iscritical"]){
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.backgroundColor = [UIColor redColor];
}
else if ([taskitem.isUrgent isEqualToString:@"isurgent"]){
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.backgroundColor = [UIColor blueColor];
}
else if ([taskitem.isCompletedOK isEqualToString:@"iscompleted"]){
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.backgroundColor = [UIColor greenColor];

    UIButton *doneButton4 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 12, 12)];
    [doneButton4 setImage:[UIImage imageNamed:@"done"]forState:UIControlStateNormal];
    [cell addSubview:doneButton4];
}
else {
       cell.textLabel.textColor = [UIColor blackColor];
   }
    cell.textLabel.text = taskitem.taskName;

The problem is that the if statements are changing their behaviour if the user taps on any of the sections headers.

Do you find any error on my code that could be the reason of this weird behaviour or should I search for another reason in another method?

It looks like you're probably having a cell reuse problem. You need to make sure that anything you do in one case, is done for each case (or undone).

For example, in first your three conditions, you set the text colour and the background colour. But in the fourth case (the else) you only set the text colour, leaving the background colour to be whatever it was on this cell the last time it was used. You need to set the background colour in the else case as well. (Optionally, you can set all items back to defaults before the if).

You have a second problem here, with the third case, that creates and adds a button to the cell. But in the other cases, you do not remove that button if it exists. So when you get a cell that was used for a completed item earlier, you'll end up with a button that doesn't belong. Even if the cell is used for another completed item, you'll get two buttons on the same cell. One on top of the other (so it likely won't be visible, but it's still a problem).

Try using this code:

UIButton* oldButton = [cell viewWithTag:253];
if (oldButton)
{
    [oldButton removeFromSuperview];
}

if ([taskitem.isCritical isEqualToString:@"iscritical"]) {
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.backgroundColor = [UIColor redColor];
}
else if ([taskitem.isUrgent isEqualToString:@"isurgent"]) {
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.backgroundColor = [UIColor blueColor];
}
else if ([taskitem.isCompletedOK isEqualToString:@"iscompleted"]) {
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.backgroundColor = [UIColor greenColor];

    UIButton *doneButton4 = [[UIButton alloc] initWithFrame:CGRectMake(0, 10, 12, 12)];
    [doneButton4 setImage:[UIImage imageNamed:@"done"] forState:UIControlStateNormal];
    doneButton4.tag = 253;
    [cell addSubview:doneButton4];
}
else {
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.backgroundColor = [UIColor whiteColor];
}

cell.textLabel.text = taskitem.taskName;

I don't recommend using tags like this in production code, but it's a quick way to see if it solves your problems. In real code, use a UITableViewCell subclass with a property for the done button that you can just show and hide. Ideally you're not creating it each time as that's an expensive operation and could slow down your scrolling performance.

Create doneButton4 in (cell == nil) when reuse cells in table view

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIButton *doneButton4 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 12, 12)];
[doneButton4 setImage:[UIImage imageNamed:@"done"]forState:UIControlStateNormal];
[cell addSubview:doneButton4];
doneButton4.tag=100;
}

UIButton *doneButton4=(UIButton*)[cell viewWithTag:100];
doneButton4.hidden=YES;
if ([taskitem.isCritical isEqualToString:@"iscritical"]){
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor redColor];
}
else if ([taskitem.isCritical isEqualToString:@"isurgent"]){
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor blueColor];
}
else if ([taskitem.isCritical isEqualToString:@"iscompleted"]){
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor greenColor];
doneButton4.hidden=NO;

}


else {
   cell.textLabel.textColor = [UIColor blackColor];
}
cell.textLabel.text = taskitem.taskName;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM