3

I have seen some posts before, but didn't get the answer yet, thats why i am trying to post again in more effective manner. How can i use check-uncheck functionality in UITableView like below image.

This is table i want when i click on button of any cell, that buttons image will change, not on all cells.

table

Community
  • 1
  • 1
iPhone Programmatically
  • 1,211
  • 2
  • 22
  • 54

5 Answers5

7

For Check-Uncheck functionality only buttonClicked: method is not enough. You will have also put the condition in cellForRowAtIndexPath: method for which button is selected or which in unselected because cellForRowAtIndexPath: method will call each time when you will scroll your UITableView and cells will be refresh. And i saw your previous question you're adding two buttons with two action not a good way just change the image of button for check-uncheck.

So here is what i do for this -

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
{
    IBOutlet UITableView *tblView;
    NSMutableArray *arrayCheckUnchek; // Will handle which button is selected or which is unselected
    NSMutableArray *cellDataArray; // this is your data array
}

@end

Now in ViewController.m class -

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    arrayCheckUnchek = [[NSMutableArray alloc]init];
    //Assign your cell data array 
    cellDataArray = [[NSMutableArray alloc]initWithObjects:@"cell-1",@"cell-2",@"cell-3",@"cell-4",@"cell-5", nil];

    // setting all unchecks initially
    for(int i=0; i<[cellDataArray count]; i++)
    {
        [arrayCheckUnchek addObject:@"Uncheck"];
    }

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [cellDataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [cellDataArray objectAtIndex:indexPath.row];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(270.0, 7.0, 30.0, 30.0)];

    if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
    [button setImage:[UIImage imageNamed:@"uncheck_icon"] forState:UIControlStateNormal];
    else
    [button setImage:[UIImage imageNamed:@"check_icon"] forState:UIControlStateNormal];

    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:button];

    return cell;
}

-(void)buttonClicked:(id)sender
{
    //Getting the indexPath of cell of clicked button

    CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tblView];
    NSIndexPath *indexPath = [tblView indexPathForRowAtPoint:touchPoint];

    // No need to use tag sender will keep the reference of clicked button
    UIButton *button = (UIButton *)sender;

    //Checking the condition button is checked or unchecked.
    //accordingly replace the array object and change the button image
    if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
    {
        [button setImage:[UIImage imageNamed:@"check_icon"] forState:UIControlStateNormal];
        [arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Check"];
    }
    else
    {
        [button setImage:[UIImage imageNamed:@"uncheck_icon"] forState:UIControlStateNormal];
        [arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Uncheck"];
    }
}

And finally it will look like -

enter image description here

TheTiger
  • 13,264
  • 3
  • 57
  • 82
  • in button action just add [[self tableView] reloadData]; Its work perfectly. – kiran Jul 15 '16 at 13:21
  • Kiran: Actually no need to add this line as I am changing button's image and and it's array. Reloading table view is needed if I do not change the image here. – TheTiger Jul 16 '16 at 13:59
0

Tags are useful with customised uitableviewcell designed in IB. If you create cells programmatically, you don't need tags. You use them only to find correct uiview to set properties.

Ivor Prebeg
  • 988
  • 6
  • 11
0

Yes this is simple to set Tags while declaring the classes inside UITableViewCell and identify them using tags. I have posted some sample code for your reference. Am not sure it will solve your problem. You asked how to set tags and identify the classes using tags. So it will give you some basic idea about your question.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        label1 = [[UIView alloc] initWithFrame:CGRectMake(2, 20, 15, 15)];
        label1.tag = 100;
        label1.backgroundColor = [UIColor whiteColor];
        label1.layer.cornerRadius = 5;
        [cell.contentView addSubview: label1];

        label2 = [[UILabel alloc] initWithFrame:CGRectMake(25, 2, 165, 23)];
        label2.tag = 101;
        label2.font = [UIFont boldSystemFontOfSize:15];
        label2.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview: label2];

        label3 = [[UILabel alloc] initWithFrame:CGRectMake(190, 2, 90, 23)];
        label3.tag = 102;
        label3.font = [UIFont systemFontOfSize:14];
        label3.textColor = [UIColor colorWithRed:0.14117670588235 green:0.435294117647059 blue:0.847058823529412 alpha:1];
        label3.backgroundColor = [UIColor clearColor];
        label3.textAlignment = UITextAlignmentRight;
        [cell.contentView addSubview: label3]; 
    }

   label1 = (UILabel *) [cell.contentView viewWithTag:100];
    NSString *string1 = [array1 objectAtIndex:indexPath.row];

  label2 = (UILabel *) [cell.contentView viewWithTag:101];
    NSString * string2 = [array2 objectAtIndex:indexPath.row];

  label3 = (UILabel *) [cell.contentView viewWithTag:102];
    NSString * string3 = [array3 objectAtIndex:indexPath.row];

   return cell;   
} 

Please let me know this is useful or not. Otherwise we'll go to some other way.

Happy Coding. Thanks.

Yuvaraj.M
  • 9,741
  • 16
  • 71
  • 100
0

i agree with @VakulSaini because u can do this to handle which cell touch or swipe or what ever and this is an ex:

-(void)handleSwipLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
    CGPoint Location = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:Location];
    cell = [tableView cellForRowAtIndexPath:indexPath];
}
Omarj
  • 1,151
  • 2
  • 16
  • 43
0

Use cellForRowAtIndexPath method of UITableView and in This method Add any component like (UITextField,UILabel ..... etc ) In your UITableViewCell by using [cell.contentView addSubview:YourComponentName]; and give each component to its tag by indexPath.row.

such like ,,,

YourComponentName.tag = indexPath.row;