查看表视图单元格按钮上的tableview单元格文本

时间:2016-09-27 05:01:20

标签: ios swift uitableview swift3

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

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

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

enter image description here

@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)

3 个答案:

答案 0 :(得分:1)

尝试获取int regular, special, vip; int i; bool flag = true; while(flag) { cout << "input please: "; cin >> i; if ((i <10000) && (i > 0)) { regular++; } if ((i >10000) && (i<=50000)) { special++; } if (i > 50000) { vip++; } if (i==0); { flag = false; cout << "Your branch has " << regular << " Regular Customers, " << special << " Special Customers and " << vip << " VIP Customers" << "Try again? [1 = yes/ 0 = no] " << endl; } } ,使用按钮标记点击按钮。

indexPath

答案 1 :(得分:1)

你需要做的事情

<强> swift3

from BeautifulSoup import BeautifulSoup
import urllib2
import urllib
import requests

img_url='http://7-themes.com/data_images/out/79/7041933-beautiful-backgrounds-wallpaper.jpg'

headers = {}
headers['Referer'] = img_url

r = requests.get(img_url, headers=headers)

with open('7041933-beautiful-backgrounds-wallpaper.jpg', 'wb') as fh:
    fh.write(r.content)

采取行动

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

<强> Swift2

 @IBAction func buttonClicked(_ sender: UIButton){

     print(self.arrayList[sender. tag])

}

答案 2 :(得分:1)

在表视图控制器中

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
}