Sam B
Sam B

Reputation: 27608

uitabelview custom button tag issue

I am creating a custom button in my uitable and giving the button a tag number as shown in code below.

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    //NSLog(@"Inside cellForRowAtIndexPath");

    static NSString *CellIdentifier = @"Cell";

    // Try to retrieve from the table view a now-unused cell with the given identifier.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // If no cell is available, create a new one using the given identifier.
    if (cell == nil)
    {
        // Use the default cell style.
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self
                   action:@selector(selectPicOnBtnTouch:)
         forControlEvents:UIControlEventTouchDown];
        [button setTitle:@"btn1" forState:UIControlStateNormal];
        button.frame = CGRectMake(6.0f, 0.0f, 97.0f, 110.0f);
        button.tag = (indexPath.row)+100;
        [cell addSubview:button];

        [button setImage:[UIImage imageNamed:@"sample_pic.png"] forState:UIControlStateNormal];

    }
    else
    {
        //NSLog(@"went here 2");
        button = (UIButton *) [cell viewWithTag:((indexPath.row)+100)];
    }

    //Get an arrow next to the name
    cell.accessoryType = UITableViewCellAccessoryNone;

    return cell;

}

- (IBAction) selectPicOnBtnTouch:(id)sender
{
    button = (UIButton *)sender;
    NSLog(@"[button tag]: %d ...", [button tag]); 
}

If I have a table with 10 rows and when I touch buttons in rows 1-5 I get the following in my log

[button tag]: 100 ...
[button tag]: 101 ...
[button tag]: 102 ...
[button tag]: 103 ...
[button tag]: 104 ...

This is fine. The problem is that when I touch row 6-10 I am expecting 105 - 109 tags but what I get back is same tag numbers starting over

[button tag]: 100 ...
[button tag]: 101 ...
[button tag]: 102 ...
[button tag]: 103 ...
[button tag]: 104 ...

The reason I want to keep the tag numbers unique (and not repeating) is so that I know which button corresponds to what row. How can I do that?

Upvotes: 0

Views: 102

Answers (3)

You need to do smth like that:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.rNewsCell = [self.tabelView dequeueReusableCellWithIdentifier:allNewsCellIdentifier forIndexPath:indexPath];

    if (self.news.count != 0)
    {
        if(indexPath.row <[self.news count])
        {
            RNewsModel *news = (self.news)[indexPath.row];
            self.rNewsCell.newsImage.contentMode = UIViewContentModeScaleAspectFill;
            self.rNewsCell.newsName.text = news.newsNameModel;
            self.rNewsCell.newsData.text = news.newsDataModel;
            self.rNewsCell.newsWatches.text = news.newsWatchesModel;
            self.rNewsCell.newsAddress.text = news.newsAddressModel;
            self.rNewsCell.newsTime.text = news.newsTimeModel;
            self.rNewsCell.playFrame.image =  [UIImage imageNamed: news.newsPlayImage];
            self.rNewsCell.playButton.tag = indexPath.row;
            self.rNewsCell.showOnMap.tag = indexPath.row;

            self.rNewsCell.rightUtilityButtons = [self rightButtons];
            self.rNewsCell.delegate = self;
        }
    }
    return self.rNewsCell;
}

and after that catch the button actions:

- (IBAction)showNewsOnTheMap:(id)sender
{
    UIButton *button=(UIButton *) sender;
    self.showNewsOnMap = [NSIndexPath indexPathForRow:button.tag inSection:0];
    RNewsModel *news = (self.news)[self.showNewsOnMap.row];
    NSLog(@"didSelect NEWSOnMap = %@",news.newsIDModel);
    [self performSegueWithIdentifier:SegueShowNewsOnTheMap sender:self];
}

Upvotes: 0

foggzilla
foggzilla

Reputation: 1725

Don't set the button tag inside the if block where you create the cell, because cells will be reused and you are not updating the tag in the scenario when the cell is reused.

EDIT : For the first 5 cells, if block is being executed and new cells are being created. 6th cell onwards the cells are being reused and hence count is not incremened.

Call your

button.tag = (indexPath.row)+100;

below your if (cell == nil) block, like this:

    if (cell == nil)
    {
        //...

    }
    else
    {
      ///...
        button = (UIButton*)[[cell subviews] lastObject]; //this assumes your button is the last view in the subview array (double check)
    }
    button.tag = (indexPath.row)+100;

Upvotes: 0

iPatel
iPatel

Reputation: 47059

Before You follow my Answer i wnat to tell you that following code is bad for memory management because it will create new cell for each rows of UITableView, so be careful for it.

But it is better to use, When UITableView Have Limited rows (about 50-100 may be ) then following code is helpful in your case, use it if is it suitable for you.

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

    NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

         /// Put your code here
     }

    return cell;
}

Upvotes: 2

Related Questions