Tableview与复选框

时间:2011-06-10 06:29:29

标签: iphone uitableview checkbox

我正在使用此代码进行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 =@"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;
}

我如何知道哪些单元格被选中以及哪些单元格何时点击某个按钮

4 个答案:

答案 0 :(得分:5)

您可以使用以下方法访问按钮操作上的tableView单元格。您可以使用 if(cell.accessoryType == UITableViewCellAccessoryCheckmark)条件检查选择,因为您要为所选单元格设置 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
            }
        }
    }
}

答案 1 :(得分: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];
  }
}

答案 2 :(得分:0)

为此,你应该拿一个数组。 当选择特定行时,您可以将indexpath.row输入到该数组中。 当选择相同的行时,您可以搜索到数组,该行存在,如果它已退出,则可以从单元格中删除复选框,并从数组中删除该indexpath.row。

所以这样你就可以获得所有选择的indexpath.row。

希望你明白这一点。

如有任何困难,请告诉我。

答案 3 :(得分:0)

你必须像这样设置

BOOL selected[array count];

然后

为所选单元格设置为true(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]];  
    }  
}  
相关问题