2011-06-10 45 views
2

我正在使用此代碼進行tableview複選框。Tableview with checkbox

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

      cell.textLabel.text [email protected]"a"; 
      int flag = (1 << indexPath.row); 
      if (_checkboxSelections & flag) 
      { 
       cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      } 


    return cell; 
} 



#pragma mark - 
#pragma mark Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
       _checkboxSelections ^= (1 << indexPath.row); 
    [tableView reloadData]; 
} 

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 10; 
} 

我怎樣才能知道哪些細胞被選中的,哪些不是當我點擊一些按鈕

+1

在didselectRowAtIndesPath中,您可以監視選定哪一行。 – Aravindhan 2011-06-10 06:34:32

回答

5

您可以使用以下方法訪問按鈕操作中的tableView單元格。如果(cell.accessoryType == UITableViewCellAccessoryCheckmark) condition,您可以使用檢查選擇,因爲您正在爲所選單元格設置UITableViewCellAccessoryCheckmark

- (void)onButtonClick { 

    int numberOfSections = [tableView numberOfSections]; 

    for (int section = 0; section < numberOfSections; section++) { 

     int numberOfRows = [tableView numberOfRowsInSection:section]; 

     for (int row = 0; row < numberOfRows; row++) { 

      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section]; 
      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

      if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 

       // Cell is selected 

      } else { 

       // Cell is not selected 
      } 
     } 
    } 
} 
0

爲此,你應該採取一個數組。 當特定行被選中時,您可以將indexpath.row輸入到該數組中。 當選擇同一行時,您可以搜索數組,該行存在,如果它退出,則可以從單元中刪除複選框,並從數組中刪除該indexpath.row。

所以這樣你可以得到所有選中的indexpath.row。

希望你明白了。

如有任何困難,請告知我。

0

你必須這樣設置

BOOL selected[array count]; 

然後

設置選定單元格(didselectrow方法)真

selected[indexPath.row] = !selected[indexPath.row]; 

在按鈕單擊您要使用

NSMutableArray *true = [NSMutableArray array]; 

for (int i = 0; i < (sizeof(selected)/sizeof(BOOL)); i++) { 
    if (selected[i]) { 
     [true addObject:[Yourarray objectAtIndex:i]]; 
    } 
} 
1
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [array count]; 
} 

-(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] ; 
    UIButton * checkBox=[UIButton buttonWithType:UIButtonTypeCustom]; 
    checkBox.tag=indexPath.row; 
    checkBox.frame=CGRectMake(270,15, 20, 20); 
    [cell.contentView addSubview:checkBox]; 
    [checkBox setImage:[UIImage imageNamed:@"checkbox_not_ticked.png"] forState:UIControlStateNormal]; 
    [checkBox addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    } 
cell.textLabel.text=[array objectAtIndex:indexPath.row]; 
return cell; 
} 

-(void)checkBoxClicked:(id)sender { 
    UIButton *tappedButton = (UIButton*)sender; 
    if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"checkbox_not_ticked.png"]]) 
    { 
    [sender setImage:[UIImage imageNamed: @"checkbox_ticked.png"] forState:UIControlStateNormal]; 
    } else { 
    [sender setImage:[UIImage imageNamed:@"checkbox_not_ticked.png"]forState:UIControlStateNormal]; 
    } 
} 
+1

我在我的項目中使用了這種方法,但是當我滾動我的TableView時,檢查框會自動取消選中。任何人都可以幫助我..! – 2014-01-13 12:43:27