简体   繁体   English

如果语句在tableView中显示奇怪的结果

[英]If statements are showing weird results in a tableView

In my cellForRowAtIndexPath method I am using following if statements: 在我的cellForRowAtIndexPath方法中,我使用以下if语句:

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. 问题在于, if statements用户点击任何节标题,则if statements将改变其行为。

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). (或者,您可以将所有项目设置为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. 在实际代码中,将UITableViewCell子类与带有done属性的属性一起使用,即可显示和隐藏。 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 在表视图中重用单元格时,在(cell == nil)中创建doneButton4

- (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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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