Sam B Sam B - 27 days ago 9
iOS Question

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?

Answer Source

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