iPhone SDK : custom buttons in TableView

标签 iphone uitableview

我在表格 View 中使用自定义按钮,它对我很有用:

 - (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。

最佳答案

看起来您希望 showDetails 了解哪个按钮按下了它。我认为你这样做的方式很好,但如果你愿意,你可以有一个功能

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

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

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

但说实话,你现在的做法很好,为什么要改变呢?

关于iPhone SDK : custom buttons in TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3486434/

相关文章:

iphone - UIScrollview缩放并同时启用分页

ios - 以编程方式设置文本后获取 UILabel 的大小

iphone - iOS 6 将数据从 TableView 传递到 Detail View

iphone - 在 iPhone 中返回上一个屏幕

iphone - 进行 Facebook 集成时出错

iphone - 在同一个 #define 中识别 iPhone 3、4 和 5

ios - 设置约束后导航栏后面的 UITableView

ios - 我可以在 AppDelegate.m 之外注册推送通知吗?

ios - 在 UITableviewcell swift 中设置水平渐变

ios - Parse.com 查询未加载