如何在 UITableView 内的 UIButton 点击​​上传递一个 int 值

Posted

技术标签:

【中文标题】如何在 UITableView 内的 UIButton 点击​​上传递一个 int 值【英文标题】:How to pass an int value on UIButton click inside UITableView 【发布时间】:2010-12-31 14:43:12 【问题描述】:

所以我有一个带有单个 UITableView 的视图控制器,它向用户显示一个字符串。但我需要一种方法让用户使用普通 UIButton 通过 id 选择此对象,因此我将其添加为子视图,并且 click 事件按预期工作。

问题是这样的——如何将一个 int 值传递给这个点击事件?我尝试使用按钮的属性,如 button.tag 或 button.accessibilityhint,但没有取得多大成功。专业人士如何传递这样的 int 值?

同样重要的是,我可以在稍后的过程中从 int 中获取实际的 [x intValue]。不久前,我想我可以用一个简单的方法来做到这一点

NSLog(@"实际选择的帽子是%d", [obj.idValue intValue]);

但这似乎不起作用(知道如何直接从变量中提取实际的 int 值吗?)。

我的工作代码如下

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    

  if ([self.hats count] > 0) 
      Hat* obj = [self.hats objectAtIndex: [indexPath row]];

    NSMutableString* fullName = [[NSMutableString alloc] init];
    [fullName appendFormat:@"%@", obj.name];
    [fullName appendFormat:@" %@", obj.type];

    cell.textLabel.text = fullName;
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

    //add the button to subview hack
    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(50, 5, 50, 25);
    [button setTitle:@"Select" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor clearColor];
    button.adjustsImageWhenHighlighted = YES;

    button.tag = obj.idValue; //this is my current hack to add the id but no dice :(

    [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];

    [cell.contentView addSubview:button];
    //hack
  

    return cell;  


- (void)action:(id)sender 
  int* hatId = ((UIButton *)sender).tag; //try to pull the id value I added above ...

  //do something w/ the hatId 

【问题讨论】:

你说 NSLog(@"实际选择的帽子是 %d", [obj.idValue intValue]);不起作用,所以也许你从来没有设置它。因为我在设置标签并在发送者的操作方法中读取它时做同样的事情,但对我来说它有效。 您需要附件按钮和“选择”按钮吗?当一个附件按钮被点击时,它会调用 accessoryButtonTappedForRowWithIndexPath 所以不需要标记。 我正在使用附件按钮查看帽子的详细信息,选择只是将特定的帽子添加到购物车以进行结帐/等 @iPortable - 好问题(我会验证这一点,但我很高兴知道其他人正在这样做) 那么你最初的问题似乎是,正如 pbcoder 指出的那样,你在 action 方法中声明了一个 int* 而不仅仅是一个 int 。但是,我会将标签设置为 indexPath.row 而不是对象中的值(这样您可以在需要时访问对象的其他属性)。此外,您正在将按钮添加到单元格而不考虑单元格重用。当您向上/向下滚动时,单元格(已经包含按钮)将被重新使用,然后将在该按钮之上添加一个新按钮。 【参考方案1】:
NSInteger hatId = ((UIButton *)sender).tag;

【讨论】:

如果使用 int,我认为你不需要 *【参考方案2】:

我会以完全不同的方式来解决这个问题。我会有一个 NSMutableDictionary ,其中包含带有标签作为键的按钮对象。可能在表的数据源中。然后,当该按钮或标签发生任何事情时,您可以轻松获得其中一个。然后,当您需要给定按钮的标签时,您可以在字典上使用allKeysForObject 方法,每个对象可能只有一个标签。如果您需要标签的按钮,您可以使用标签作为键值来获取按钮指针。这是假设标签值没有被重用并且是唯一的(如果它们是你必须在按钮消失时将它们从字典中清除)。

或者,您也可以继承 UIButton 并更适当地存储标记值。我认为理想主义者会告诉您这就是答案,就您如何尝试使其与您的 hack 一起工作而言。 =)

希望对一些人有所帮助。

【讨论】:

【参考方案3】:

最终的解决方案包括一些变化:

1.) 我没有使用 int 类型,而是改用 NSNumber(感谢一些 cmets) 我还发现 int 在内存管理方面更成问题,因为我是新手 :(

2.) 我没有将标签设置为 id,而是使用了 cmets 中提到的一些概念,并在稍后将其从操作中拉出时推送整个对象以获取有关它的更多详细信息。

这是我设置对象的修改方法

  if ([self.hats count] > 0) 
      Hat* obj = [self.hats objectAtIndex: [indexPath row]];

    NSMutableString* fullName = [[NSMutableString alloc] init];
    [fullName appendFormat:@"%@", obj.name];
    [fullName appendFormat:@" %@", obj.type];

    cell.textLabel.text = fullName;
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

    //add the button to subview hack
    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(50, 5, 50, 25);
    [button setTitle:@"Select" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor clearColor];
    button.adjustsImageWhenHighlighted = YES;

    button.tag = obj;

    [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];

    [cell.contentView addSubview:button];
    //hack
  

    return cell;  

这是修改后的操作,我拉出对象,然后是我想要的任何属性

- (void)action:(id)sender 
  Hat* obj = ((UIButton *)sender).tag;
  NSNumber* hatId = obj.idValue;
  //then if I wanted the actual value of hatId I could do
  NSLog(@"print the hatId out as is %d", [hatId intValue]);

关于按钮重用的最后一条评论 - 我还没有找到一个运行问题,但我对另一种解决方案感兴趣,它仍然允许我在每个表格单元格中都有一个自定义按钮。

【讨论】:

以上是关于如何在 UITableView 内的 UIButton 点击​​上传递一个 int 值的主要内容,如果未能解决你的问题,请参考以下文章

如何同步 UICollectionViewCell 内的 UITableView 之间的滚动(在 UICollectionViewController 内)

如何在 UITabBarViewController 内的 UINavigationController 内调整 UITableView 的大小?

如何在 UITableView 内的 UICollectionView 内获取数组数组并返回正确数量的项目?

如何添加拉表视图以刷新 uitableview 内的数据

UIViewController 内的 UITableView? [关闭]

如何检测 UIViewController 内的 UITableView 类?