0

Trying to implement the checkmark for UITableView. Checkmark for UITableView Cell is not selecting to all row, when scroll tableview its not not enable.

Below is my code which i Implemented.

IndexButton is UIButton Class which added index 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 Need suggestion, how to go forward to implement the check mark for tableview.

4

2 回答 2

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];
 }
于 2015-04-08T05:19:31.720 回答
0

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

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

// 将选定行的信息保留在可变索引设置为的部分中

NSMutableIndexSet *selctedCellsInSection;

//初始化上面的set实例

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(保留有关选定/未选定单元格的信息)重新加载表格视图。

重新加载表格将反映带有单元格的附件复选标记的选定单元格。

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

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

  [tableView reloadData];
}
于 2015-04-08T05:14:22.237 回答