2011-04-04 42 views
10

我有UITableView,每行都包含一個使用UITableViewCellAccessoryCheckmark的複選框。我無法弄清楚如何使用didSelectRowAtIndexPath方法取消選中所有複選框。如何使用UITableViewCellAccessoryCheckmark取消選中所有行

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *oldCell; 

    int count = [self.myTableRowNamesArray count]; 

    for (NSUInteger i = 0; i < count; ++i) {         
     // Uncheck all checkboxes 
     // OF COURSE THIS DOESN'T WORK 
     // BECAUSE i IS AN INTEGER AND INDEXPATH IS A POINTER 
     FOO: oldCell = [myTableView cellForRowAtIndexPath:(int)i]; 
     // GOOD CODE: 
     oldCell = [penanceOptionsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; 
     oldCell.accessoryType = UITableViewCellAccessoryNone; 
    } 
    UITableViewCell *newCell = [myTableView cellForRowAtIndexPath:indexPath]; 
    newCell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 

回答

4

是,cellForRowAtIndexPath:使用NSIndexPath,而不是整數,從而通過使用

indexPathForRow:inSection: 

如果您使用的一個部分,然後你的循環是罰款只是通過我行和0的部分使indexpath。

11
for (UITableViewCell *cell in [myTableView visibleCells]) { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 

但實際上,只是修改實際上具有複選標記集的一個單元格會更好。無論如何,您必須將這些信息存儲在模型的某個位置。

+1

這就是我的意思。只需更改具有複選標記的單元格(當然還有正在成爲新選中單元格的單元格)。 – 2011-04-04 19:00:31

20

而不是修改所有細胞的.accessoryTypedidSelectRowAtIndexPath:的,我建議存儲所選擇的索引在一些實例變量,並改變在數據源的-tableView:cellForRowAtIndexPath:方法.accessoryType,即

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath { 
    self.selectedIndexPath = indexPath; 
    [tableView reloadData]; 
} 

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { 
    ... 
    cell.accessoryType = [indexPath compare:self.selectedIndexPath] == NSOrderedSame 
          ? UITableViewCellAccessoryCheckmark 
          : UITableViewCellAccessoryNone; 
    ... 
} 

由此,只可見單元格將受到影響,屏幕外的其他單元格不需要修改。


沒錯,這裏是在選擇電池一般情況下,斯威夫特完全實現..你,你認爲合適的其他地方使用selectedIndexPath在類。例如,在cellForRowAtIndexPath中選擇適當的單元格原型。

// SelectingTableViewController 

import UIKit 

class SelectingTableViewController: UITableViewController 
    { 
    internal var selectedIndexPath:NSIndexPath? = nil 

    override func viewDidLoad() 
     { 
     super.viewDidLoad() 
     tableView.estimatedRowHeight = 68.0 
     tableView.rowHeight = UITableViewAutomaticDimension 

     self.clearsSelectionOnViewWillAppear = false; 
     } 

    override func tableView 
     (tableView:UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) 
      { 
      print("did select....") 

      // in fact, was this very row selected, 
      // and the user is clicking to deselect it... 
      // if you don't want "click a selected row to deselect" 
      // then on't include this clause. 
      if selectedIndexPath == indexPath 
       { 
       print("(user clicked on selected to deselect)") 
       selectedIndexPath = nil 
       tableView.reloadRowsAtIndexPaths(
        [indexPath], 
        withRowAnimation:UITableViewRowAnimation.None) 

       tableView.deselectRowAtIndexPath(indexPath, animated:false) 
       return 
       } 

      // in fact, was some other row selected?? 
      // user is changing to this row? if so, also deselect that row 
      if selectedIndexPath != nil 
       { 
       let pleaseRedrawMe = selectedIndexPath! 
       // (note that it will be drawn un-selected 
       // since we're chaging the 'selectedIndexPath' global) 
       selectedIndexPath = indexPath 
       tableView.reloadRowsAtIndexPaths(
        [pleaseRedrawMe, indexPath], 
        withRowAnimation:UITableViewRowAnimation.None) 
       return; 
       } 

      // no previous selection. 
      // simply select that new one the user just touched. 
      // note that you can not use Apple's willDeselectRowAtIndexPath 
      // functions ... because they are freaky 
      selectedIndexPath = indexPath 
      tableView.reloadRowsAtIndexPaths(
       [indexPath], 
       withRowAnimation:UITableViewRowAnimation.None) 

      } 

    } 
+1

多次調用[reloadData]會導致任何不好的結果? – onmyway133 2013-11-07 04:57:40

+0

top markotop(y) – tesmojones 2015-08-14 08:45:09

5

你可能會用這種方法設置某種屬性。 所以我要做的就是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // 1. first unsetting the property 
    [object someProperty:nil]; 

    // 2. call the reloadData method to uncheck all the checkmarks 
    [tableView reloadData]; 

    // 3. check the selected cell 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 

    // 4. set the checked property 
    [object setSomeProperty:[indexpath row]]; 
} 

而且在我的cellForRowAtIndexPath方法我有類似下面的代碼:

if([object someProperty] == [indexpath row]){ 
     [cell setAccessoryType:UITableViewCellAccessoryCheckmark];   
    } else { 
     [cell setAccessoryType:UITableViewCellAccessoryNone]; 
    } 
相關問題