views:

108

answers:

1

I'm using custom buttons in Table view and it works good for me:

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

     UIImage *detailsButtonImage;
     UIButton *detailsButton;

     NSString *CellIdentifier = @"Cell";

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
     }

     //populate cells with array elements
     cell.textLabel.text = [itemsArray objectAtIndex:indexPath.row];

     //a custom button
     detailsButton = [UIButton buttonWithType:UIButtonTypeCustom];
     detailsButtonImage = [UIImage imageNamed:@"details.png"];
     [detailsButton setBackgroundImage:detailsButtonImage forState:UIControlStateNormal];
     detailsButton.frame = CGRectMake(275, 10, 20, 22);

      //which cell was tapped?
     [detailsButton setTag:[itemsArray objectAtIndex:indexPath.row]];
     [detailsButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
     [cell.contentView addSubview:detailsButton];
      return cell;
 }

//showing details of selected item
- (void) showDetails:(id)sender
{  
    AnotherViewController *anotherViewController = [[AnotherViewController alloc] init];
    anotherViewController.title = [sender tag];
    anotherViewController.itemDescription = [itemsDescriptions objectAtIndex:[itemsArray indexOfObjectIdenticalTo:[sender tag]]]; 

    [self.navigationController pushViewController:anotherViewController animated:YES];
    [anotherViewController release];
}

I'm curious if there is another way of sending a cell identifier to AnotherViewController except setting a tag.

+1  A: 

It looks like you are want showDetails to know which button pushed it. I think the way you are doing it is fine, but if you wanted you could have a function

  • (void) showDetails:(id)sender withObject:(id)object;

and call that with your button rather than just showDetails:(id)sender; I am inferring that your objects are strings from what you are doing, so it could be

  • (void) showDetails:(id)sender withString:(NSString *)string;

But honestly the way you are doing it right now is fine, why change?

fogelbaby
I've heard "setTag" could be replaced with sending an object through button target but I'm puzzled because there is already one target set.
ufw