2010-08-15 86 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