[英]Cell accessorytype doesn't reload on [tableview reloadData]
我對iPhone編程非常陌生,所以我的問題可能是由於對基本原理的完全了解而引起的。
我正在開發具有兩個視圖的iPhone應用程序。 第一個視圖有兩個按鈕,當按下其中一個按鈕時,將彈出一個帶有表格視圖的模式視圖。 根據所按下的按鈕,此表視圖的填充方式有所不同。 如果按下按鈕button1,則將用button1Data填充表格視圖。 用戶可以選擇單元格,如果完成,則將單元格的附件類型設置為UITableViewCellAccessoryCheckmark。 然后,我將已檢查單元格的名稱保存到tableViewListChecked中,以便以后可以確定隨着數據的變化應該檢查哪個單元格。
問題如下:關閉了modalview后,我選擇了button2,在button1Data中選擇的單元格仍在button2Data中選擇。 對我來說很明顯,[theTableView reloadData]函數正在工作,因為數據確實發生了變化,但是,在我將單元格從屏幕上滾動下來之前,accesorytupes仍然是相同的。
附言:如果我確實在屏幕上滾動單元格,則附件類型設置正確。
- (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]];
}
}
感謝您的幫助!
如果單元格不是nil(如果它已從表視圖中出隊),則實際上並不需要更改附件類型。 刪除其他,即更改
else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
至
if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
聽起來您每次呈現模態視圖時都在重用UITableView
的相同實例,在這種情況下, tableViewListChecked
數組發生了什么? 您正在交換tableViewList
數據; 但是您是否采用某種策略來在button1和button2的水龍頭之間保留已檢查項目的列表?
如果這里的人沒有幫助您,您可以設置:cell.accessoryType = UITableViewCellAccessoryNone每次用戶單擊button2時
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.