简体   繁体   中英

Passing data from TableViewController to TableViewCell

I created a TableViewController and I'm displaying some data coming from an Array. I had to create a TableViewCellController also, because i need to have a button for each row, which has to perform an action.

So, this is a part of my TableViewController:

struct person {
  var name:String
  var age:String
  var address:String
}
class myTableViewController: UITableViewController {
  var people = Array<person>()

  override func viewDidLoad() {
    super.viewDidLoad()

    var x = person(name: "Marco", age: "28", address: "street a")
    var y = person(name: "Pippo", age: "90", address: "street b")
    people.append(x)
    people.append(y)
  }

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> myTableViewCell {
    let CellId: String = "Cell"
    var cell: myTableViewCell = tableView.dequeueReusableCellWithIdentifier(CellId) as myTableViewCell
    cell.textLabel!.text = people[indexPath.row].name
    return cell
  }

}

The table is printed fine as you can see in the following picture:

表格视图控制器

Now, i need to access the property address every time i press the button "Address" which you can see in the picture. So i tried with the following code, inside class TableViewController:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> myTableViewCell {
  let CellId: String = "Cell"
  var cell: myTableViewCell = tableView.dequeueReusableCellWithIdentifier(CellId) as myTableViewCell
  cell.textLabel!.text = people[indexPath.row].name

  cell.buttonPlay?.tag = indexPath.row
  cell.buttonPlay?.addTarget(cell, action: "playActionX:", forControlEvents: UIControlEvents.TouchUpInside)

  return cell
}

and, from TableViewCell i correctly receive the tag:

class TableViewCell: UITableViewCell {

  func playActionX(sender:UIButton!) {
    println(sender.tag)
  }

Everything fine. But the problem is, i cannot send strings inside the tag and I don't know how to access the 'address' field from my TableViewController.

What solution could i use for that? PS. I have tried to create an instance to the TableViewController class:

func playActionX(sender:UIButton!) {
  println(sender.tag)
  let instance:MyTableViewController = MyTableViewController()
  println(instance.getPeopleArray())
}

(getPeopleArray is just returning the array people) but strangely the array i receive it's empty and i don't understand why.

Thanks for your support

Your getPeopleArray is empty because you are creating a new instance of MyTableViewController, not accessing the existing one. But don't go that route. Instead just change your target/action for the buttons to your view Controller, not the cell. So change:

cell.buttonPlay?.addTarget(cell, action: "playActionX:", forControlEvents: UIControlEvents.TouchUpInside)

to

cell.buttonPlay?.addTarget(self, action: "playActionX:", forControlEvents: UIControlEvents.TouchUpInside)

and move the playActionX: function to the table view controller. From there you can use the tag as an index into your people array.

Since you set the tag = indexPath.row , you will be able to find the address the same way its done in cellFoRowAtIndexPath , that is: people[sender.tag].address .

The reason you're finding the array empty in your attempt is that you've allocated a new one along with the new view controller.

只要您在cellForRowAtIndexPath中分配按钮标签,就应该能够使用people[sender.tag] (在playActionX内部playActionX )访问people数组中的对象。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM