簡體   English   中英

如果語句在tableView中顯示奇怪的結果

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

在我的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;

問題在於, if statements用戶點擊任何節標題,則if statements將改變其行為。

您是否在我的代碼上發現任何可能是這種奇怪行為的原因的錯誤,還是應該在另一種方法中尋找其他原因?

看來您可能遇到了單元重用問題。 您需要確保在每種情況下(或撤消)在每種情況下都可以完成任何操作。

例如,在您的三個條件中,首先設置文本顏色和背景顏色。 但是在第四種情況(其他情況)中,您僅設置了文本顏色,而使背景顏色保持為上次使用此單元格時的背景顏色。 您還需要在其他情況下設置背景色。 (或者,您可以將所有項目設置為if之前的默認值)。

在第三種情況下,這里有第二個問題,它會創建一個按鈕並將其添加到單元格。 但是在其他情況下,請勿刪除該按鈕(如果存在)。 因此,當您較早獲得用於完成項目的單元格時,您將得到一個不屬於該按鈕的按鈕。 即使該單元格用於另一個完成的項目,您也會在同一單元格上獲得兩個按鈕。 一個在另一個之上(因此它可能不可見,但這仍然是一個問題)。

嘗試使用以下代碼:

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;

我不建議在生產代碼中使用這樣的標簽,但這是查看它是否可以解決您的問題的快速方法。 在實際代碼中,將UITableViewCell子類與帶有done屬性的屬性一起使用,即可顯示和隱藏。 理想情況下,您不必每次都創建它,因為這是一項昂貴的操作,並且可能會降低滾動性能。

在表視圖中重用單元格時,在(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