UITableView的复选标记不是选择所有TableViewCell

时间:2015-04-08 04:44:45

标签: ios objective-c iphone uitableview checkmark

尝试实施UITableView的复选标记。 滚动表格视图时,UITableView单元格的选中标记未选择所有行 它不是没有启用。

以下是我实施的代码。

IndexButton是UIButton类,它添加了索引init。

-(void)selectAllAction:(IndexedButton *)sender{    

    for (int rowIndex = 0; rowIndex < [array_MedicineList count]; rowIndex++) {        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowIndex  inSection:0]; 
        UITableViewCell *cell = [tbl_ProductList cellForRowAtIndexPath:indexPath];
        IndexedButton *btn_SelectItem = (IndexedButton *)[cell viewWithTag:TAG_SELECTEDITEM];
        [btn_SelectItem setBackgroundImage:[UIImage imageNamed:@"checkMark"] forState:UIControlStateNormal];
    } 
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *productListTableViewCell = @"ProductListTableViewCell";
    ProductListTableViewCell *cell = (ProductListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:productListTableViewCell];
    if (cell == nil){

        cell = [[ProductListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                               reuseIdentifier:productListTableViewCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        IndexedButton *btn_SelectItem = [IndexedButton buttonWithType:UIButtonTypeCustom];
        btn_SelectItem.frame = CGRectMake(10,52,32,32);
        [btn_SelectItem setBackgroundImage:[UIImage imageNamed:@"uncheckMark"] forState:UIControlStateNormal];
        [btn_SelectItem addTarget:self action:@selector(selectItemAction:)forControlEvents:UIControlEventTouchUpInside];
        btn_SelectItem.index = (int)indexPath.row;
        btn_SelectItem.tag = TAG_SELECTEDITEM;

        [cell addSubview:btn_SelectItem];

    }

    IndexedButton *btn_SelectItem  = (IndexedButton *)[cell viewWithTag:TAG_SELECTEDITEM];
    btn_SelectItem.index = (int)indexPath.row;
    cell.backgroundColor = [UIColor clearColor];

    return cell;
}

@All 需要建议,如何继续实施tableview的复选标记。

2 个答案:

答案 0 :(得分:0)

我建议您使用带有UITableViewCellAccessoryCheckmark类型的附件视图的单元格来显示所选的所有单元格/选择的几个单元格/没有选择的单元格。

此外,您必须保持某个部分中每个单元格索引的状态,无论它是否被选中

//将可变索引集中的选定行的信息保存为

NSMutableIndexSet *selctedCellsInSection;

//初始化上面的设置实例

selctedCellsInSection = [[NSMutableIndexSet alloc] init];

//索引路径中行的单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
{
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
 }
    if ([selctedCellsInSection containsIndex:indexPath.row])
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    else 
        [cell setAccessoryType:UITableViewCellAccessoryNone];

    // customize cell as per your requirements

    return cell;
}

您需要保存有关单元格复选标记的信息是否需要在selctedCellsInSection中显示为 -

使用[selctedCellsInSection addIndex:rowToSelect] //添加需要显示复选标记的单元格索引

使用[selctedCellsInSection removeIndex:rowToUnselect] //添加不应显示复选标记的单元格索引

之后,自定义数据源selctedCellsInSection(保留有关所选/未选定单元格的信息)重新加载tableview。

重新加载表格将使用Cell的附件选中标记反映所选单元格。

在您的情况下,您需要在所有单元格上显示复选标记,您可以这样做 -

-(void)showCheckMarkOnAllCells
{    
    for (int rowIndex = 0; rowIndex < [array_MedicineList count]; rowIndex++) 
    {        
        [selctedCellsInSection addIndex: rowIndex];
    } 

  [tableView reloadData];
}

答案 1 :(得分:0)

@interface BRNCategoryViewController ()
{      
  NSMutableArray *arySelectCategory;
  NSMutableArray *aryCategory;
}

 - (void) viewDidLoad
{
     arySelectCategory=[NSMutableArray new];
}


 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
     return aryCategory.count;
 }



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BRNCategoryCell *cell=[[BRNCategoryCell alloc]initWithOwner:self];

     if ([arySelectCategory containsObject:[aryCategory objectAtIndex:indexPath.row]])
      {
           cell.imgBoxView.image=[UIImage imageNamed:@"checkMark"];
      }
      else
      {
            cell.imgBoxView.image=[UIImage imageNamed:@"uncheckMark"];
      }
            cell.lblTitle.textColor=Rgb2UIColor(127, 127, 127);
            cell.lblTitle.font=[ASCustomClass FontWithSize:20.0];
            cell.lblTitle.text=aryCategory[indexPath.row];
            cell.backgroundColor=[UIColor clearColor];
            return cell;


  }



 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
      [tableView deselectRowAtIndexPath:indexPath animated:YES];

      if ([arySelectCategory containsObject:[aryCategory objectAtIndex:indexPath.row]])
      {
           [arySelectCategory removeObject:[aryCategory objectAtIndex:indexPath.row]];
      }
      else
      {
           [arySelectCategory addObject:[aryCategory objectAtIndex:indexPath.row]];
      }
            [tblEventCategory reloadData];
 }