Question

I want to show a custom checkmark and uncheckmark image for tableview cell when user select any row and when a new row is inserted cell is loaded with uncheckmark image.

Once I check the row then even after reloading table with new data I want to display a previously checked row as it is and new as unchecked.

How can I get all rows with checked mark at end.

I tried but I am not able to load first time with custom checkmark image I can change when row is deselected but its also having some issues as it is not consistently working as I mentioned above.

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;

 }

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

   return [cardData.mArrRecipient count];

 }

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

    static NSString * sCellIdentifier = @"cell";
    NSLog(@"cellforrow");


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sCellIdentifier];
    if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sCellIdentifier] autorelease];

    RecipientData *recipientData = [[cardData.mArrRecipient objectAtIndex:indexPath.row]autorelease];
    cell.textLabel.text = recipientData.sRecipientFName;
} 
cell.accessoryType=UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
 {

    if (IndexPath) 
    {
        UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_checkmark_red.png"]] autorelease];
        [tableView cellForRowAtIndexPath:indexPath].accessoryView = imageView;
        IndexPath=FALSE;
    }
    else 
    {
        UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_uncheckmark_red.png"]] autorelease];
        [tableView cellForRowAtIndexPath:indexPath].accessoryView = imageView;
        IndexPath=TRUE; 
    }
  }


  -(void)viewWillAppear:(BOOL)animated
  {

[super viewWillAppear:animated];   
if ([cardData.mArrRecipient count]>0) 
{
    [IBTblNewRecipientDetail reloadData];        
}
 }

can any one help me on this??

Was it helpful?

Solution

here is tutorial http://weheartgames.com/2009/06/simple-iphone-checkbox/

or use this

  UIButton   * checkBox=[UIButton buttonWithType:UIButtonTypeCustom];
        checkBox.tag=indexPath.row;
        checkBox.frame=CGRectMake(270,15, 20, 20);
        [cell.contentView addSubview:checkBox];
        [checkBox setImage:[UIImage imageNamed:@"selectedBoxWith.png"] forState:UIControlStateNormal];
        [checkBox addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];

-(void)checkBoxClicked:(id)sender{
if(self.isChecked ==NO){
  self.isChecked =YES;
  [sender  setImage:[UIImage imageNamed: @"selectedBoxWithTik.png"] forState:UIControlStateNormal];
}else
{
   self.isChecked =NO;
  [sender setImage:[UIImage imageNamed:@"selectedBoxWith.png"]forState:UIControlStateNormal];

    }
}


and 

-(void)checkBoxClicked:(id)sender{

    UIButton *tappedButton = (UIButton*)sender;

    if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"selectedBoxWith.png"]]) {
        [sender  setImage:[UIImage imageNamed: @"selectedBoxWithTik.png"] forState:UIControlStateNormal];
    }

    else {
        [sender setImage:[UIImage imageNamed:@"selectedBoxWith.png"]forState:UIControlStateNormal];
    }
}

OTHER TIPS

For selecting multiple rows:

- (void)addTableView {

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake("whatever frame you want to set")
                                                        style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;


self.timeSheetEntryTableView.allowsMultipleSelection = YES;

self.array = [[NSMutableArray alloc]init];
[self.view addSubview:self.tableView];

}

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

NSString *cellIdentifier = @"Cell Identifier";


self.cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];


if (!self.cell) {

self.cell = [[Cell alloc]initWithStyle:UITableViewCellStyleDefault
                               reuseIdentifier:cellIdentifier];
self.cell.accessoryType = UITableViewCellAccessoryNone;

}
self.cell.selectionStyle = UITableViewCellSelectionStyleNone;
if ([self.array containsObject:indexPath])
{
[self.cell.checkImage setImage:[UIImage imageNamed:@"check_box_selected.png"]];
}
else
{
 [self.cell.checkImage setImage:[UIImage imageNamed:@"check_box.png"]];
}

return self.cell;

}

//did select and deselect I have used custom cell

 - (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

self.cell = [tableView cellForRowAtIndexPath:indexPath];

[self.array addObject:indexPath];
[self.cell.checkImage setImage:[UIImage imageNamed:@"check_box_selected.png"]];

}

- (void)tableView:(UITableView *)tableView 
didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

self.cell = [tableView cellForRowAtIndexPath:indexPath];

[self.array removeObject:indexPath];

[self.cell.checkImage setImage:[UIImage imageNamed:@"check_box.png"]];

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top