我有一个UITableView(每行都有一个名为CellModelAllNames的自定义类)。每行都有一个标签和一个按钮。

我的问题是:当btn_addRecording(即在任一行/每行上单击“+”按钮时,如何获取lbl_name.text,显示的标签名称以及如何在ViewController本身中显示弹出窗口。我想在弹出窗口中获取其他信息,然后保存所有信息(包括lbl_name到数据库)。

每个行布局的CellModelAllNames:

import UIKit


class CellModelAllNames: UITableViewCell {

@IBOutlet weak var lbl_name: UILabel!

@IBOutlet weak var btn_addRecording: UIButton!


@IBAction func btnAction_addRecording(sender: AnyObject) {

    println("clicked on button in UITableViewCell")

}
override func awakeFromNib() {
    super.awakeFromNib()
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
}

func setCell(setBabyName: String) {
    self.lbl_name.text = setBabyName
}
}

这是我的ViewController的代码:
import UIKit

class SecondViewController: UIViewController, UITableViewDelegate,   UITableViewDataSource {


@IBOutlet weak var tbl_allNames: UITableView!

var arrayOfNames: [Name] = [Name]()

override func viewDidLoad() {
    super.viewDidLoad()
    self.tbl_allNames.delegate = self
    self.tbl_allNames.dataSource = self
    self.tbl_allNames.scrollEnabled = true

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell:CellModelAllNames = self.tbl_allNames.dequeueReusableCellWithIdentifier("CellModelAllNames") as! CellModelAllNames
    let name = arrayOfNames[indexPath.row]
      cell.setCell(name.name)
     println("in tableView, cellforRowatIndex, returning new cells")
     return cell
 }
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrayOfNames.count
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

}


}

最佳答案

您可以使用标准的UIKit方法来获取单元格及其数据:

func tappedButton(sender : UIButton) {
   let point = sender.convertPoint(CGPointZero, toView: self.tableView)
   let indexPath = self.tableView.indexPathForRowAtPoint(point)!
   let name = arrayOfNames[indexPath.row]
   // do something with name
}

关于ios - 如何处理UITableView每行中每个按钮的按钮单击,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29736459/

10-14 23:18
查看更多