2010-12-01 27 views
0

我編程一個UITableView表現爲一個包容性的選擇列表。我的表格正確顯示並允許用複選框選擇多個單元格。我的問題是,已經選擇的單元格(單元格右側有一個複選標記)在滾動出視圖時鬆開了它們的選定狀態(單元格複選標記消失)。我希望即使單元格滾動出視圖,對錶格中單元格的選擇也會被保留。有誰知道這是什麼原因造成的?問題與配置的UITableView的包容性選擇列表

這裏是我的代碼我TableViewController類的內部:

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

NSUInteger row = [indexPath row]; 
cell.textLabel.text = [widgetTitles_glob objectAtIndex:row]; 
cell.detailTextLabel.text = @""; 
cell.textLabel.textColor = [UIColor blackColor]; 
cell.textLabel.font = [UIFont boldSystemFontOfSize:15]; 
cell.accessoryType = UITableViewCellAccessoryNone; 
return cell; 

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES]; 
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
if (cell.accessoryType == UITableViewCellAccessoryNone) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    // Reflect selection in data model 
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    // Reflect deselection in data model 
} 

}

任何幫助將是非常讚賞。

回答

0

在cellForRowAtIndexPath中:u將assassining的accessoryType設置爲none ...因此,當whenevr u滾動代理被調用,並將附件類型設置爲none ...所以你應該改變你的代碼。

我也面臨這一PRB一次...我來了與如下的溶液..

商店選擇indexPath的在陣列中(此代碼應在didSelectRowAtIndexPath方法代表),如果它的值indexPath.row是取消選擇從該陣列中刪除..在cellForRowAtIndexPath:方法我已經使用了for循環,並檢查是否存在indexPath.row然後更改其配件類型爲複選標記否則沒有

+0

應該不是如果(細胞==零)條件保存表中的所有單元格,一旦他們被分配? – Sabobin 2010-12-01 16:54:49

+0

你不應該只存儲indexPath.row,你應該存儲整個indexPath。如果你檢查'if([selectedRows containsObject:indexPath.row])cell.accessoryType = UITableViewCellAccessoryCheckmark; 其他 單元格。accessoryType = UITableViewCellAccessoryNone;'那麼當您滾動時,每個部分中的所有第一行將最終被複選標記。你應該保存整個indexPath,因爲它同時包含了section和row。 – tazboy 2011-08-10 03:20:50

0

感謝您的幫助。它實際上原來,爲什麼細胞得到復位UITableViewCellAccessoryNone的原因是becasue的cellForRowAtIndexPath內以下行的代碼:

cell.accessoryType = UITableViewCellAccessoryNone; 

刪除此有固定的表。

+1

不,這不會解決它...現在當你滾動時,檢查會隨機出現/消失。 – benzado 2010-12-01 17:06:31

1

當你正確使用的UITableView,因爲需要以適應屏幕上只有儘可能多的UITableViewCell實例分配。當您向下滾動一個表格,並且一個單元格從屏幕頂部消失時,它將重新定位到底部。

您的代理方法tableView:cellForRowAtIndexPath:負責設置單元格,或者創建一個新單元格或,重新配置回收單元格

做正確的事情是使用一個數組來存儲您選中/取消值。當調用didSelectRowAtIndexPath:時,您更新單元格和您的數組。當調用tableView:cellForRowAtIndexPath:時,可以根據數組中的值配置單元。

根據您的意見,您已經在做正確的事didSelectRowAtIndexPath:;您只需在設置單元實例時使用這些值,因爲該單元可能表示已檢查過的行。檢查數組,然後相應地設置cell.accessoryType