繁体   English   中英

如何在UIButton上传递int值在UITableView中单击

[英]How to pass an int value on UIButton click inside UITableView

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

问题是这样的-如何将int值传递给此click事件? 我试过使用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 
}
NSInteger hatId = ((UIButton *)sender).tag;

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

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

希望对您有所帮助。

最终解决方案包括一些更改:

1.)我没有使用int类型,而是切换到NSNumber(由于提出了一些评论),我还发现int在内存管理方面存在更多问题,因为我是新手:(

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM