Search code examples
iphoneuitableviewdidselectrowatindexpathcheckmark

iPhone : How can we load a custom check mark image for row in tableview?


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??


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