在表格视图单元格按钮上查看表格视图单元格文本 [英] View tableview cell text on table view cell button
本文介绍了在表格视图单元格按钮上查看表格视图单元格文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表格视图,我在其中创建了一个标签和两个按钮.我在单击按钮时从标签中获取文本时卡住了.我创建了一个数组列表,如:
I have a table view where I have created a label and two buttons. I am stuck while getting the text from the label on button click. I have created an array list like:
let arrayList: [String] = [ "aaa" , "bbb" , "ccc"]
我想如果我点击 index[0]
上的按钮,我会得到aaa",如果 index[2]
我会得到ccc"
I want if I click the button on index[0]
I shall get "aaa" and if index[2]
I shall get "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)
推荐答案
在你的表视图控制器中
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
}
这篇关于在表格视图单元格按钮上查看表格视图单元格文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文