2011-05-19 52 views
6

我有一個UITableView填充了27行。我正在嘗試更改所選單元格的accessoryType。我在didSelectRowAtIndexPath:委託方法中這樣做。didSelectRowAtIndexPath一次選擇多個行

我面臨的問題是,當選擇一行並更改單元格的accessoryType時,該行的第11行也會被修改。

我試過打印[indexPath row]的值,但它只顯示被選中的行而不是另一個。

我很困惑這樣的東西;請幫助我。

添加的代碼cellForRowAtIndexPath方法

UITableViewCell *cell; 
if ([indexPath row] == 0) { 
    cell = [tableView dequeueReusableCellWithIdentifier:@"acell"]; 
} 
else { 
    cell = [tableView dequeueReusableCellWithIdentifier:@"bcell"]; 
} 

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

if (cell == nil && [indexPath row] != 0) { 
    cell = [[[UITableViewCell alloc] initWithStyle: 
      UITableViewCellStyleSubtitle reuseIdentifier:@"bcell"] autorelease]; 
} 
else if(cell == nil && [indexPath row] == 0){ 
    cell = [[[UITableViewCell alloc] initWithStyle: 
      UITableViewCellStyleSubtitle reuseIdentifier:@"acell"] autorelease]; 
} 

if ([cell.contentView subviews]){ 
    for (UIView *subview in [cell.contentView subviews]) { 
     [subview removeFromSuperview]; 
    } 
} 
if ([indexPath row] == 0) { 
    cell.textLabel.text = @"Select All"; 
    cell.textLabel.font = [UIFont boldSystemFontOfSize:13.0f]; 
} 
else { 
    cell.textLabel.text = @"Some Text Here" 
    cell.detailTextLabel.text = @"Another piece of text here" 
} 
return cell; 

我做%10因爲行爲11行後重復,因此試圖爲每11行創建新的對象。

didSelectRowAtIndexPath methos代碼

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    if ([indexPath row] != 0) { 
     NSIndexPath *tempIndex = [NSIndexPath indexPathForRow:0 inSection:0]; 
     UITableViewCell *tempCell = [tableView cellForRowAtIndexPath:tempIndex]; 
     tempCell.accessoryType = UITableViewCellAccessoryNone; 
    } 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 
else{ 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
if ([indexPath row] == 0) { 
    for (int i = 0; i < [dataSource count]; i++) { 
     NSIndexPath *tempIndex = [NSIndexPath indexPathForRow:i+1 inSection:0]; 
     UITableViewCell *tempCell = [tableView cellForRowAtIndexPath:tempIndex]; 
     if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
      tempCell.accessoryType = UITableViewCellAccessoryCheckmark; 
     } 
     else{ 
      tempCell.accessoryType = UITableViewCellAccessoryNone; 
     } 
    } 
} 

請幫我多選或任何其它的方式來解決多重選擇的問題。

在此先感謝!

+4

發佈您的代碼可能是?最有可能你正在重用cellFor錯誤在cellForRowAtIndexPath方法 – Vladimir 2011-05-19 11:21:29

+0

張貼您的cellforrowindex tableview代碼 – 2011-05-19 11:26:20

+0

檢查http://stackoverflow.com/questions/4616345/select-multiple-rows-in-uitableview/4616432例如 – Vladimir 2011-05-19 12:35:57

回答

15

下面是做這件事:

- (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.textLabel setText:[NSString stringWithFormat:@"Row %d", indexPath.row]]; 

    NSIndexPath* selection = [tableView indexPathForSelectedRow]; 
    if (selection && selection.row == indexPath.row) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    // Configure the cell. 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
} 

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
} 

記得在表視圖中的每一個細胞實際上是被重新使用相同的對象。如果每次調用cellForRowAtIndexPath時都沒有設置附件類型,當新的單元格滾動到屏幕上時,它們都將具有相同的附件。

多重選擇

對於多重選擇它更復雜一些。

你的第一個選項:未公開的API

注意,這隻能在表的編輯模式。將每個單元格的編輯樣式設置爲未記錄的UITableViewCellEditingStyleMultiSelect。一旦你這樣做了,你可以通過一個UITableView的非文檔成員來獲取表視圖的選擇:indexPathsForSelectedRows。這應該返回所選單元格的數組。

你可以把這個在頭部暴露的功能位:

enum { 
    UITableViewCellEditingStyleMultiSelect = 3, 
}; 

@interface UITableView (undocumented) 
- (NSArray *)indexPathsForSelectedRows; 
@end 

然後設置,像這樣每個單元格的編輯風格:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return UITableViewCellEditingStyleMultiSelect; 
} 

當表處於編輯模式您會看到單元格上的多選控件。

翻閱其他未公開的API,你可以使用nm命令行實用程序是這樣的:

nm /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/System/Library/Frameworks/UIKit.framework/UIKit 

你的第二個選項:管理選擇自己

有你的UITableView子類包含一個數組這表明選擇了哪些單元格。然後在cellForRowAtIndexPath中,使用該數組配置單元格的外觀。然後,您的didSelectRowAtIndexPath方法方法應該是這個樣子:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([tableView indexPathIsSelected:indexPath]) { 
     [tableView removeIndexPathFromSelection:indexPath]; 
    } else { 
     [tableView addIndexPathToSelection:indexPath]; 
    } 
    // Update the cell's appearance somewhere here 
    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
} 

這裏假設你在你的UITableView子類中創建indexPathIsSelected,removeIndexPathFromSelection和addIndexPathToSelection方法。這些方法應該完全按照它們的名稱暗示:添加,刪除和檢查數組中的索引路徑。如果使用此選項,則不需要執行didDeselectRowAtIndexPath。

+0

嘿謝謝你了,如果只做單一選擇,這段代碼工作得很好。如果我需要實施多重選擇,我應該改變什麼?謝謝 – devsri 2011-05-19 12:12:53

+0

有你去。答案已更新。 :) – daxnitro 2011-05-19 13:57:56

+0

只做了一些測試,並修改了多選部分(無證API的東西)。 – daxnitro 2011-05-19 14:41:34

0

請記住,表視圖中的每個單元格實際上是重複使用的同一個對象。如果您沒有設置輔助型每一次的cellForRowAtIndexPath被調用時,當新細胞滾動到他們將所有的屏幕具有相同的附件「 - daxnitro

這是我被抓住了我。有我的設置,以便在我的「cellForRowAtIndexPath」函數中,我只會更改我的已檢查單元格陣列中指定的那些附件,而我應該完成的工作是更新表格中所有單元格的附件。換句話說:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
//normal set up 

    //retrieve key 
    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults]; 
    id obj = [settings objectForKey:@yourKey]; 

    //if the array is not populated, keep standard format for all cells 
    if (obj == nil){ 
     selectedStyles = [[NSMutableArray alloc] initWithObjects:nil]; 
     [cell setAccessoryType:UITableViewCellAccessoryNone]; //no check mark 
     [cell textLabel].textColor = [[UIColor alloc] initWithRed:0.0/255 green:0.0/255 blue:0.0/255 alpha:1.0]; //keep black color 

    } 
    //else retrieve information from the array and update the cell's accessory 
    else{ 
     //if the cell is in your array, add a checkbox 
     [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; //add check box 
     [cell textLabel].textColor = [[UIColor alloc] initWithRed:50.0/255 green:79.0/255 blue:133.0/255 alpha:1.0]; //change color of text label 
     //if the cell is not in your array, then keep standard format 
     [cell setAccessoryType:UITableViewCellAccessoryNone]; //no check mark 
     [cell textLabel].textColor = [[UIColor alloc] initWithRed:0.0/255 green:0.0/255 blue:0.0/255 alpha:1.0]; //keep black color 

希望這會有所幫助!