2010-08-15 84 views
2

我使用表视图自定义按钮自定义按钮,它为我的作品好:iPhone SDK:在TableView中

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

我很好奇,如果有发送小区标识符AnotherViewController只是另一种方式设置标签。

回答

1

它看起来像你想showDetails知道哪个按钮推它。我认为你这样做的方式很好,但如果你想要的话,你可以有一个功能

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

并用你的按钮而不是showDetails调用:(id)sender;我推断你的对象是你正在做的事情的字符串,所以它可能是

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

但说实话,你现在正在做的方式是好的,为什么要改变?

+0

我听说“setTag”可以替换为通过按钮目标发送一个对象,但我很困惑,因为已经有一个目标设置。 – ufw 2010-08-15 23:41:52