user1376698
user1376698

Reputation: 11

Multiple Check Box in UITableview iPhone

I am new in iOS I am creating dynamically buttons in my tableview i had set a image on button for check and uncheck What i have to do is when i tap(or check) on button the data on indexpath row will add in my array. And if i deselect it , data removes from my array. Please Help me

-(void)btnForCheckBoxClicked:(id)sender
{

UIButton *tappedButton = (UIButton*)sender;

indexForCheckBox= [sender tag];


if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"checkbox_unchecked.png"]]) 
{

    [sender  setImage:[UIImage imageNamed: @"checkbox_checked.png"] forState:UIControlStateNormal];
    strinForCheckBox= [ApplicationDelegate.ArrayForSearchResults objectAtIndex:indexForCheckBox];
    [arrForCheckBox addObject:strinForCheckBox];
    NSLog(@"Sender Tag When Add %d", indexForCheckBox);
    NSLog(@"Array Count Check Box %d",[arrForCheckBox count]);

}

else
{

        [sender setImage:[UIImage imageNamed:@"checkbox_unchecked.png"]forState:UIControlStateNormal];
        [arrForCheckBox removeObjectAtIndex:indexForCheckBox];
        NSLog(@"Sender Tag After Remove %d", indexForCheckBox);
        NSLog(@"Array Count Uncheck Box %d",[arrForCheckBox count]);


   }


}

Upvotes: 0

Views: 3949

Answers (4)

Praveenkumar
Praveenkumar

Reputation: 24476

I have found another way. Code extracted from @The Saad And, i had just modified with key/value pairs as useful one like below code -

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    if ([[selectedRowsArray objectAtIndex:indexPath.row] containsObject:@"YES"])
        cell.imageView.image = [UIImage imageNamed:@"checked.png"];
    else
       cell.imageView.image = [UIImage imageNamed:@"unchecked.png"];

    UITapGestureRecogniser *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleChecking:)
    [cell.imageView addGestureRecognizer:tap];
    [tap release];

    cell.textLabel.text = [contentArray objectAtIndex];
    return cell;
}

- (void) handleChecking:(UITapGestureRecognizer *)tapRecognizer {
    CGPoint tapLocation = [tapRecognizer locationInView:self.tableView];
    NSIndexPath *tappedIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];

    if ([[selectedRowsArray objectAtIndex:tappedIndexPath.row] containsObject:@"YES"])
        [selectedRowsArray replaceObjectAtIndex:tappedIndexPath.row withObject:[NSSet setWithObject:@"NO"]];
    else
        [selectedRowsArray replaceObjectAtIndex:tappedIndexPath.row withObject:[NSSet setWithObject:@"YES"]];

    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tappedIndexPath] withRowAnimation: UITableViewRowAnimationFade];
}

Add, the objects for selectedRowsArray as below with the count of your self.tableView's row count. So, you need to add the bool values as below

for ( int i = 0; i < tableViewRowCount; i++ ) { // tableViewRowCount would be your tableView's row count
    ....
    ....
    ....
    [selectedRowsArray addObject:[NSSet setWithObject:@"NO"]];
    ....
    ....
}

Hope this helps! Cheers!

Update

With the use of updated code you don't need to keep key/value pair of NSMutableDictionary for overkilling that. NSSet provides you better way with this.

Upvotes: 0

Haris
Haris

Reputation: 915

I think this question has been answered some time ago! Check it out first and tell us if this is what you are looking for ?

How to add checkboxes to UITableViewCell??

image http://img208.yfrog.com/img208/6119/screenshotkmr.png

Upvotes: 1

Saad
Saad

Reputation: 8947

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    if ([selectedRowsArray containsObject:[contentArray objectAtIndex:indexPath.row]) {
        cell.imageView.image = [UIImage imageNamed:@"checked.png"];
    }
    else {
       cell.imageView.image = [UIImage imageNamed:@"unchecked.png"];
    }
    UITapGestureRecogniser *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleChecking:)
    [cell.imageView addGestureRecognizer:tap];
    [tap release];

    cell.textLabel.text = [contentArray objectAtIndex];
    return cell;
}

- (void) handleChecking:(UITapGestureRecognizer *)tapRecognizer {
    CGPoint tapLocation = [tapRecognizer locationInView:self.tableView];
    NSIndexPath *tappedIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];

    if (selectedRowsArray containsObject:[contentArray objectAtIndex:tappedIndexPath.row]) {
        [selectedRowsArray removeObject:[contentArray objectAtIndex:tappedIndexPath.row]];
    }
    else {
        [selectedRowsArray addObject:[contentArray objectAtIndex:tappedIndexPath.row]];
    }
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tappedIndexPath] withRowAnimation: UITableViewRowAnimationFade];
}

Upvotes: 2

vishiphone
vishiphone

Reputation: 750

You can add this on your button click method.

First set Bool value which set yes on one click and No In second click and when you first click add that index value in array using array addobject property and other click removeobject property.

-(void)list
{

if(isCheck==YES)
{
    //add array object  here
    isCheck=NO;
}
else if(isCheck==NO)
{
  //remove array object here
   isCheck=YES;
}

}

Upvotes: 0

Related Questions