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.

kiran
  • 4,285
  • 7
  • 53
  • 98
  • Show your code for the `cellForRowAtIndexPath` method. It needs to set the checkmark for any checked cells. – rmaddy Apr 08 '15 at 04:57
  • it is happen because when you used indexpath for get any cell it not work because indexpath is give value of visible cell only not all cell. Some time it return null. So if you want select all facility then give tag to each row and get it by tag. – Chirag Shah Apr 08 '15 at 05:02
  • I will edit my question for CellForRowAtIndexPAth – kiran Apr 08 '15 at 05:05
  • Are you want to select all table row on click of button?If yes then do it by maintaining flag. – Bhoomi Jagani Apr 08 '15 at 05:07
  • Leave about the code what is requirement ? – Arun Apr 08 '15 at 05:10
  • Have you specified selection multiple in your table view through interface builder or programmatically?? – Mrugesh Tank Apr 08 '15 at 05:10
  • its easy just take one array with 0 and 1 value change all value to 1 when you want to select all and reload the table or change all to 0 when deselect and reload table. :) – Tejas Ardeshna Apr 08 '15 at 05:14

2 Answers2

0

I would suggest you to use cell with accessory view with UITableViewCellAccessoryCheckmark type to show all cells selected/ few cells selected/ none of the cells selected.

Also, you must keep the state for each cell index within a section, whether it's selected or not as

// keeps info for selected rows in a section in mutable index set as

NSMutableIndexSet *selctedCellsInSection;

// initialize the above set instance

selctedCellsInSection = [[NSMutableIndexSet alloc] init];

//Inside cell for row at index path

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

You need to hold the information about a cell's checkmark whether it needs to be shown or not in selctedCellsInSection set as -

Use [selctedCellsInSection addIndex:rowToSelect] // to add cell index on which checkmark needs to be shown

Use [selctedCellsInSection removeIndex:rowToUnselect] // to add cell index on which checkmark should not be shown

After, customizing the data source selctedCellsInSection(which keeps information about selected/ unselected cell) reload the tableview.

Reloading the table will reflect the selected cells with Cell's Accessory Checkmark.

In your case as you need show check mark on all cell, you can do so as-

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

  [tableView reloadData];
}
Sanjay Mohnani
  • 5,947
  • 30
  • 46
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];
 }
Rahul Mayani
  • 3,761
  • 4
  • 25
  • 40
  • Don't just post code. Explain what was wrong and explain how your answer solves the problem. – rmaddy Apr 08 '15 at 16:00