【问题标题】:How to set Action for UIButton in UITableViewCell如何在 UITableViewCell 中为 UIButton 设置 Action
【发布时间】:2013-03-27 04:51:06
【问题描述】:

我有XIB 文件TimerCell.xibUITableViewCell。在 cellForRowAtIndexPath 的其他类中,我初始化了这个 UITableViewCell:

    static NSString *CellIdentifier = @"cellTimer";
    TimerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        //cell = [[TimerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimerCell"owner:self options:nil];
        cell = [nib objectAtIndex:0];

在我的 TimerCell 中,我有两个 UILabel 和一个 UIButton。对于这个按钮,我想将 action 设置为某种方法。

我该怎么做?以及如何在第一个UILabel中实时显示我后台倒计时的数据?

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    这段代码对你有帮助

    static NSString *CellIdentifier = @"cellTimer";
    TimerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        //cell = [[TimerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimerCell"owner:self options:nil];
        cell = [nib objectAtIndex:0];
        UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.tag=indexPath.row;
       [button addTarget:self 
           action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown];
       [button setTitle:@"cellButton" forState:UIControlStateNormal];
        button.frame = CGRectMake(80.0, 0.0, 160.0, 40.0);
        [cell.contentView addSubview:button];
       }
    
      return cell;
    }
    
    
    -(void)aMethod:(UIButton*)sender
    {
     NSLog(@"I Clicked a button %d",sender.tag);
    }
    

    希望对你有帮助!!!

    【讨论】:

    • 它有效!谢谢!!现在如何为它设置标签?之前是 cell.staticButton.tag = indexPath.row;
    【解决方案2】:

    由于您有一个名为TimerCellUITableViewCell 子类,您可以将出口属性添加到TimerCell。例如:

    @interface TimerCell : UITableViewCell
    
    @property (nonatomic, strong) UIButton *button;
    @property (nonatomic, strong) UILabel *label;
    
    @end
    

    TimerCell.xib 中,将插座连接到按钮和标签。

    然后,在tableView:cellForRowAtIndexPath: 中,您可以轻松访问按钮以设置其目标和操作:

    static NSString *CellIdentifier = @"cellTimer";
    TimerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimerCell"owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    [cell.button addTarget:self action:@selector(cellButtonWasTapped:)
        forControlEvents:UIControlEventTouchUpInside];;
    

    您可以使用单元格的label 属性访问单元格的标签,以便在计时器触发时对其进行更新。

    另一种获取按钮和标签的方法是使用视图标签,正如我在this answer 中描述的那样。

    cellButtonWasTapped:(或您从按钮发送的任何操作)中,您可能想要查找包含该按钮的单元格的索引路径。我在this answer 中解释了如何做到这一点。

    【讨论】:

    • 我不明白如何在 UITableViewCell 中显示当前计时器值...你能再解释一下吗?有关更多详细信息:计时器在单例中工作。只有 1 个计时器,但有一个字典,其中包含多个包含时间值的项目。每分钟计时器从字典中的所有项目中减 1。 TimerCell 是一个 tableView 的单元格,它出现在弹出窗口中。所以我想在我的 cell.label 中显示字典项目的时间值...谢谢
    • +1 以获得出色的解释和文档……还有 10 个等着你 119,000 ;)
    【解决方案3】:
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"cellTimer";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
        NSInteger index = indexPath.row;
        UILabel *titleLabel = (UILabel *)[cell viewWithTag:101];
        UIButton *button = (UIButton *)[cell viewWithTag:100];
        [button addTarget:self action:@selector(MethodName:) forControlEvents:UIControlEventTouchUpInside];
    }
    

    并在你的XIB文件TimerCell.xib中设置UIButton和UILabel的标签

    【讨论】:

      【解决方案4】:

      在发布到 SO 之前,请进行更好的研究。 这是将按钮添加到单元格的代码

      UIButton *Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      [Button addTarget:self action:@selector(MethodName:) forControlEvents:UIControlEventTouchUpInside];
      [Button setTitle:@"ButtonTitle" forState:UIControlStateNormal];
      Button.frame = CGRectMake(100.0, 10.0, 40.0, 40.0);
      [cell.contentView addSubview:Button];
      

      【讨论】:

      • 为什么它被否决了?请解释一下,否则没用。这是错的吗?它是回答还是试图回答问题?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-11
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      相关资源
      最近更新 更多