【问题标题】:View tableview cell text on table view cell button在表格视图单元格按钮上查看表格视图单元格文本
【发布时间】:2016-09-27 05:01:19
【问题描述】:

我有一个表格视图,其中创建了一个标签和两个按钮。我在单击按钮时从标签中获取文本时被卡住了。我创建了一个数组列表,如:

let arrayList: [String] = [ "aaa" , "bbb" , "ccc"]

如果我点击index[0] 上的按钮,我会得到“aaa”,如果index[2] 我会得到“ccc”

@IBOutlet weak var titleLable: UILabel!
@IBOutlet weak var infoButton: UIButton!

myCell.titleLable.text = self.arrayList[indexPath.row]
myCell.infoButton.tag = indexPath.row
myCell.infoButton.addTarget(self, action: "buttonClicked", forControlEvents: .TouchUpInside)

【问题讨论】:

  • 你的动作没有参数,将按钮传递给它,然后从按钮标签你可以得到“aaa”或“ccc”。
  • 我已经这样做了,但我想通过点击信息按钮来显示相应的文本
  • 将标签赋予故事板和 swift 文件中的两个按钮,然后检查是否在 if 条件下使用该标签单击按钮,然后使用 firstObject 和 lastObject 的数组属性在按钮的标题文本中分配数组元素. @SajjadAims

标签: ios swift uitableview swift3


【解决方案1】:

尝试获取indexPath,使用按钮标记单击按钮。

@IBAction func buttonClicked(sender:UIButton) {
    let cell = tableView.cellForRowAtIndexPath(NSIndexPath.init(forRow: sender.tag, inSection: 0))
    cell.myLabel.text = arrayList[sender.tag]
}

【讨论】:

    【解决方案2】:

    你需要这样做

    swift3

    myCell.titleLable.text = self.arrayList[indexPath.row]
    myCell.infoButton.tag = indexPath.row
    myCell.infoButton.addTarget(self, action: #selector(yourVCName.buttonClicked(_:)), for: .touchUpInside)
    

    采取行动

     @IBAction func buttonClicked(_ sender: UIButton){
    
         print(self.arrayList[sender. tag])
    
    }
    

    Swift2

    myCell.titleLable.text = self.arrayList[indexPath.row]
    myCell.infoButton.tag = indexPath.row
    myCell.infoButton.addTarget(self, action: "buttonClicked:", forControlEvents: .TouchUpInside)
    
    
    @IBAction func buttonClicked(sender: UIButton){
    
         print(self.arrayList[sender. tag])
    
    }
    

    【讨论】:

    【解决方案3】:

    在您的表格视图控制器中

    let dataSource: [String] = [ "aaa" , "bbb" , "ccc"]
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCellWithIdentifier(YourCellIdentifier, forIndexPath: indexPath) as! YourCell
        let title = dataSource[indexPath.row]
        cell.setup(withTitle: title, delegate: self)
        return cell
    }
    
    // MARK: - Your Cell Delegate
    func didTapActionButton(fromCell cell: UITableViewCell) {
        if let indexPath = itemTable.indexPathForCell(cell) {
            let selectedItem = dataSource[indexPath.row]
            print(selectedItem)
        }
    }
    

    在您的表格视图单元格中

    首先,定义一个协议:

    protocol YourTableViewCellDelegate {
        func didTapActionButton(fromCell cell: UITableViewCell)
    }
    

    然后:

    // MARK: - Properties
    var delegate: YourTableViewCellDelegate?
    
    // MARK: - @IBOutlets
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var infoButton: UIButton!
    
    // MARK: - @IBActions
    @IBAction func buttonClicked(sender: UIButton) {
        delegate?.didTapActionButton(fromCell: self)
    }
    
    // MARK: - Public Methods
    func setup(withTitle title: title, delegate: YourTableViewCellDelegate?) {
        titleLabel.text = title
        self.delegate = delegate
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      • 2018-09-22
      相关资源
      最近更新 更多