上重新加載我對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]];
}
}
感謝您的幫助!
感謝您的回答時間,但是應用程序崩潰,如果我這樣做。這裏的控制檯輸出http://pastie.org/2677067你可能會理解它,但我沒有那麼長時間到我的「訓練」。 – AndHeiberg
可能因爲你有[NSString stringWithString:cell.textLabel.text]其中cell.textLabel.text是零,這引發了一個異常。 – jbat100
TAmazing,我把if語句移到了我設置cell.text的點以下。在這一點上它工作得很好。謝謝,你剛剛救了我的一天。 – AndHeiberg