user3131561
user3131561

Reputation: 5

actions of the buttons of the cells

i am working with the tableview in which i added 2 buttons on one cell. Below is the code which i used

- (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.backgroundView =[[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"list-bg.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
    tickbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    tickbtn.tag = 200+indexPath.row;
    [tickbtn setBackgroundImage:[UIImage imageNamed:@"ok_gray.png"]forState:UIControlStateNormal];
    [tickbtn addTarget:self action:@selector(addshed:) forControlEvents:UIControlEventTouchUpInside];
    tickbtn.frame = CGRectMake(220, 10, 30, 30);
    [cell.contentView addSubview:tickbtn];
    NSLog(@"tickbtn tag %ld",(long)tickbtn.tag);

    crossbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    crossbtn.tag = 400+indexPath.row;
    [crossbtn setBackgroundImage:[UIImage imageNamed:@"delete-gray.png"]forState:UIControlStateNormal];
    [crossbtn addTarget:self action:@selector(removeshed:) forControlEvents:UIControlEventTouchUpInside];
    crossbtn.frame = CGRectMake(250, 10, 30, 30);
    [cell.contentView addSubview:crossbtn];
    NSLog(@"tickbtn tag %ld",(long)crossbtn.tag);

    return cell;
}

on the tickbtn and crossbtn i am applying following actions :-

-(IBAction)addshed:(UIControl *)sender
{
      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag-200 inSection:0];
    UITableViewCell *cell = (UITableViewCell*)[list_table cellForRowAtIndexPath:indexPath];
    UIButton *check1 = (UIButton*)[cell.contentView viewWithTag:indexPath.row+200];
    UIButton *check2 = (UIButton*)[cell.contentView viewWithTag:indexPath.row+400];
    UIImageView *btnimg1 = [[UIImageView alloc] initWithImage:check1.currentBackgroundImage];
    //UIImageView *btnimg2 = [[UIImageView alloc] initWithImage:check2.currentBackgroundImage];
    NSLog(@"SHED LIST subviews: %@", btnimg1.image);
    // Shed_data *sheddata  = [[Shed_data alloc] init];
    if (btnimg1.image == [UIImage imageNamed:@"ok_gray.png"]) {
        //btnimg.image = [UIImage imageNamed:@"ok_gray.png"];
        [check1 setBackgroundImage:[UIImage imageNamed:@"ok_green.png"]forState:UIControlStateNormal];
        [check2 setBackgroundImage:[UIImage imageNamed:@"delete-gray.png"]forState:UIControlStateNormal];
        [self addsheddata:sender];
        NSLog(@"tickbtn tag %ld",(long)tickbtn.tag);
    }
     else if (btnimg1.image == [UIImage imageNamed:@"ok_green.png"])
     {
        [check2 setBackgroundImage:[UIImage imageNamed:@"delete-red.png"]forState:UIControlStateNormal];
        [check1 setBackgroundImage:[UIImage imageNamed:@"ok_gray.png"]forState:UIControlStateNormal];
        [self removesheddata:sender];

}


}
-(IBAction)removeshed:(UIControl*)sender
{
  //.…………………….. My functionality
}

but in both these cases i am getting the tag value of last cell only whenever i am pressing the buttons of the cell. Please locate my error and help me out to solve it. Your help will be much appreciable.

Upvotes: 0

Views: 114

Answers (4)

Mani
Mani

Reputation: 17585

I have seen issue. It may be lead to this type of error.

Why do you add subviews again and again to your cell's content view.?

That is, for every cellForRowAtIndexpath: call, button will be add to cell. In case dequeueReusableCellWithIdentifier:, your last cell may reuse to any other cell while scroll. It will lead to your error. that is your cell will contain two button.(tag with last cell and tag with your current cell).

In this line [cell.contentView addSubview:tickbtn];, you have to do some change according to add once and also for crossbtn.

Updation: I have seen your updated question. My suggestion, better use custom cell. Use this link to how to create custom cell.. Lot of confusion in your code. ex. in this line UITableViewCell *cell = (UITableViewCell*)[list_table cellForRowAtIndexPath:indexPath];, It will give unexpected. Don't call delegate method like this.

Upvotes: 0

Buntylm
Buntylm

Reputation: 7373

Try this one as working fine for me. I Just tested with my Xcode 5.

Modification : 1. I Create an NSMutableArray with the name of _objects (_objects = [[NSMutableArray alloc]initWithObjects:@"one",@"two",@"thre", nil];). and give it to my UITableView.

2.Give the tickBtn and crossBtn an different color so easily visible.

3.change the button pressed function to UIControl to UIButton like -(IBAction)addshed:(UIButton *)sender and when button pressed i catch the tag value and print it out on the console.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

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

    cell.backgroundView =[[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"list-bg.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
    tickbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    tickbtn.tag = 200+indexPath.row;
    [tickbtn setBackgroundImage:[UIImage imageNamed:@"ok_gray.png"]forState:UIControlStateNormal];
    [tickbtn setBackgroundColor:[UIColor blackColor]];
    [tickbtn addTarget:self action:@selector(addshed:) forControlEvents:UIControlEventTouchUpInside];
    tickbtn.frame = CGRectMake(220, 10, 30, 30);
    [cell.contentView addSubview:tickbtn];
    NSLog(@"tickbtn tag %ld",(long)tickbtn.tag);

    crossbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    crossbtn.tag = 400+indexPath.row;
    [crossbtn setBackgroundImage:[UIImage imageNamed:@"delete-gray.png"]forState:UIControlStateNormal];
    [crossbtn addTarget:self action:@selector(removeshed:) forControlEvents:UIControlEventTouchUpInside];
    crossbtn.frame = CGRectMake(250, 10, 30, 30);
    [crossbtn setBackgroundColor:[UIColor greenColor]];

    [cell.contentView addSubview:crossbtn];
    NSLog(@"tickbtn tag %ld",(long)crossbtn.tag);

    return cell;
}

-(IBAction)addshed:(UIButton *)sender   {
    NSLog(@"add shed %d",sender.tag);
}

-(IBAction)removeshed:(UIButton *)sender   {
    NSLog(@"remove %d",sender.tag);
}

NEW QUESTION UPDATE

Did you try with 10 or more cells and try with some continuous scroll?

And the result is

enter image description here enter image description here

As the Another Answer says

[cell addSubview:crossbtn];// -------- Change here ---------

Let me clear this as i know about it.

The contentView is a subview of UITableViewCell. please review this reference and here you can see there are actually 3 subviews in a UITableViewCell.

Upvotes: 1

Piyush Dubey
Piyush Dubey

Reputation: 2414

Your code seems fine for cellForRowAtIndexpath:.
Error might be in getting the tag value at button click. Try to change with this code:-

-(IBAction)addshed:(UIControl *)sender
{
    //.…………………….. My functionality

    int selectedRow1 = ((UIControl *)sender).tag;
    NSLog(@"No. %d", selectedRow1);

}

Upvotes: 0

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

You need to add your button to cell subview not cell's contentview subview. So use this code....

- (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.backgroundView =[[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"list-bg.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
        tickbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        tickbtn.tag = 200+indexPath.row;
        [tickbtn setBackgroundImage:[UIImage imageNamed:@"ok_gray.png"]forState:UIControlStateNormal];
        [tickbtn addTarget:self action:@selector(addshed:) forControlEvents:UIControlEventTouchUpInside];
        tickbtn.frame = CGRectMake(220, 10, 30, 30);
        [cell addSubview:tickbtn];// -------- Change here ---------
        NSLog(@"tickbtn tag %ld",(long)tickbtn.tag);

        crossbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        crossbtn.tag = 400+indexPath.row;
        [crossbtn setBackgroundImage:[UIImage imageNamed:@"delete-gray.png"]forState:UIControlStateNormal];
        [crossbtn addTarget:self action:@selector(removeshed:) forControlEvents:UIControlEventTouchUpInside];
        crossbtn.frame = CGRectMake(250, 10, 30, 30);
        [cell addSubview:crossbtn];// -------- Change here ---------
        NSLog(@"tickbtn tag %ld",(long)crossbtn.tag);

        return cell;
    }

Upvotes: 0

Related Questions