【问题标题】:How to set a tag value to UIButton in UITableView Cell in iOS?如何在 iOS 的 UITableView Cell 中为 UIButton 设置标签值?
【发布时间】:2015-11-19 10:40:19
【问题描述】:

我使用自定义单元格并在UITableViewCell 中创建了UIButton。我想更改按钮状态,如下图所示。我是 Xcode 的新手,任何人都可以指导我。提前致谢

这是我的代码

PrivacyCell.h

@property (strong, nonatomic) IBOutlet UIButton *on_offBtn;

- (IBAction)on_offBtnAction:(UIButton *)sender;

@property (nonatomic,assign) BOOL on_image;

@property (nonatomic,assign) BOOL off_image;

PrivacyCell.m

#import "PrivacyCell.h"

- (IBAction)on_offBtnAction:(UIButton *)sender {
     if (on_image == YES) {
         [_on_offBtn setImage:[UIImage imageNamed:@"on_icon.png"]                      forState:UIControlStateNormal];
         on_image = NO;
     } else if (on_image == NO)  {
         [_on_offBtn setImage:[UIImage imageNamed:@"off_icon.png"] forState:UIControlStateNormal];
         on_image = YES;
     }
}

【问题讨论】:

  • 如何填充 UITableView ?
  • 请添加您的cellForRow 部分
  • 显示你的cellforRowAtIndexPath

标签: ios objective-c uitableview uibutton


【解决方案1】:

cellforRowAtIndexPath设置按钮属性 Like

btn.tag = indexPath.row;
[btn setImage:[UIImage imageNamed:@"off_btn.png"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"on_btn.png"] forState:UIControlStateSelected];

并在点击事件中改变其状态

- (IBAction)on_offBtnAction:(UIButton *)sender
{
     sender.selected = !sender.selected;
     // [yourTableVew reloadData];
}

【讨论】:

  • 完整的解决方案在这里。请参考此链接stackoverflow.com/questions/33432578/…
  • 我使用CustomCell来显示btn。我应该如何设置
  • [btn setImage:[UIImage imageNamed:@"off_btn.png"] forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:@"on_btn.png"] forState:UIControlStateSelected];
【解决方案2】:

你可以在这里设置标签

PrivacyCell.m

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self)
    {
       on_offBtn.tag = 100;
    }
    return self;
 }

然后你可以使用

UIButton *button = (UIButton *)[cell viewWithTag:100];
button.selected = true;

但您也可以使用您的财产。

【讨论】:

    【解决方案3】:

    即使您可以在cellForRowAtIndexPath 中将标签设置为UIButton

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
       static NSString *cellIdentifier = @"Cell";
    
       PrivacyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
       if (cell == nil) {
          cell = [[PrivacyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
          // or you can use custom method for init
       }
    
       ...
    
       cell.on_offBtn.tag = indexPath.row;
    
       ...
    
       return cell;
    
    }
    

    【讨论】:

      【解决方案4】:

      您可以从情节提要中为 UITableViewCell 中的按钮创建一个 IBAction,然后每当您单击该按钮时,都会调用此方法。您将获得您单击的按钮的索引,您可以相应地执行任何任务。

      如果您在 tabelViewCell 上有多个按钮,这会有所帮助。

      - (IBAction)tidesButtonAction:(id)sender
      {
           CGPoint buttonPosition = [sender convertPoint:CGPointZero
                                             toView:self.tableView];
           NSIndexPath *tappedIP = [self.tableView indexPathForRowAtPoint:buttonPosition];
           NSString *text = [tidesURLArray objectAtIndex:tappedIP.row];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-12
        • 1970-01-01
        相关资源
        最近更新 更多