2011-10-10 70 views
1

上重新加載我對iPhone編程非常陌生,所以我的問題可能是由於完全缺乏對基本原理的知識所致。Cell accessorytype不能在[tableview reloadData]

我正在開發一個有兩個視圖的iPhone應用程序。第一個視圖有兩個按鈕,當按下其中一個按鈕時,模式視圖會彈出一個tableview。這個表格視圖的填充方式取決於按下哪個按鈕。如果按鈕button1被按下,tableview被填充button1Data。用戶可以選擇單元格,如果已完成,單元格accessorytype將設置爲UITableViewCellAccessoryCheckmark。然後我將選中的單元格的名稱保存到tableViewListChecked中,以便稍後可以決定在數據更改時應該檢查哪個單元格。

問題如下:modalview關閉後,我選擇button2在button2Data中選中的單元仍然在button2Data中選中。我很清楚,由於數據確實發生了變化,函數[theTableView reloadData]正在工作,但是在我將這些單元格從屏幕上滾動之前,accesorytupes仍然是相同的。

P.S.如果我確實在屏幕上滾動單元格,那麼accessorytype設置正確。

- (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] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 

    else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    cell.textLabel.text = [tableViewList objectAtIndex:indexPath.row]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    if (![tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     [tableViewListChecked addObject:[NSString stringWithString:cell.textLabel.text]];  
    } 

    else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     [tableViewListChecked removeObject:[NSString stringWithString:cell.textLabel.text]]; 
    } 
} 

感謝您的幫助!

回答

1

如果單元格不爲零(如果它已從表視圖中出列),那麼您實際上不會改變附件類型。降否則,即改變

else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 

if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
+0

感謝您的回答時間,但是應用程序崩潰,如果我這樣做。這裏的控制檯輸出http://pastie.org/2677067你可能會理解它,但我沒有那麼長時間到我的「訓練」。 – AndHeiberg

+0

可能因爲你有[NSString stringWithString:cell.textLabel.text]其中cell.textLabel.text是零,這引發了一個異常。 – jbat100

+0

TAmazing,我把if語句移到了我設置cell.text的點以下。在這一點上它工作得很好。謝謝,你剛剛救了我的一天。 – AndHeiberg

0

這聽起來像你正在重用的UITableView的每次展示模態視圖同一時刻,在這種情況下,發生了什麼事到tableViewListChecked數組?您正在交換tableViewList中的數據;但是你是否採用了一些策略來在按鈕1和按鈕2之間的點擊之間保持已檢查項目的列表?

+0

tableViewListChecked數組是由我分配的,據我所知,它不會被刪除,直到程序退出。陣列不需要在退出後保存,所以我根本不會釋放它。這是我的策略,它可能不是理想的,但到目前爲止它工作得很好。 感謝您的回答。 – AndHeiberg

0

如果這裏的人沒有幫你,你可以設置: cell.accessoryType = UITableViewCellAccessoryNone每次用戶點擊按鈕2個

+0

我喜歡你的想法,但我不知道如何設置每個細胞accesorrytype從其他veiw沒有。你能向我解釋一下嗎? 感謝您的回覆。 – AndHeiberg

+0

嚴..沒有測試過我的想法,但我認爲你可以在viewDidLoad中定義一個布爾值並將其設置爲NO。然後當用戶點擊button2時,將此布爾值設置爲YES並將其保存在userDefaults中(如果您不知道如何顯示代碼),則當另一個視圖出現時,請從存儲器中檢查此bool,並在您的cellForRowAtIndexPath方法檢查這個布爾值並設置你的單元格..正如我之前提到 –

+0

這可能是一個解決方案,但我不會測試它,因爲jBat已經給了我一個很好的答案,這將讓我保持在模態視圖控制器的所有代碼:)但再次感謝 – AndHeiberg