2015-12-20 66 views
1

我有一個自定義UITableViewCell它有一個開關,並有切換事件連線到表格單元,所以我可以捕捉事件。自定義UITableViewCell數據

下面是代碼:

import UIKit 

class SwitchTableCell: UITableViewCell { 
    @IBOutlet weak var title: UILabel! 
    @IBOutlet var cellSwitch: UISwitch! 

    @IBAction func switchValueChanged(sender: UISwitch) { 
     print("Switch changed") 
     print("**") 
     print(self.title.text!) 
     print(self.cellSwitch.on) 
     print("**") 

    } 
} 

我得到交換機正確的標題文本和布爾值。

現在,當開關事件觸發時,我需要調用我的ViewController中引用被觸發的單元格的方法。我想在我的控制器(ViewController.swift)的方法,我可以打電話的時候,表格單元格開關切換是這樣的:

func tableCellChange(titleText: String, state: Boolean){ 
    // Do something with the object represented by titleText 
} 

獎金是如果我能有在表格單元格中DoorEntity成員而跳過從名稱

func tableCellChange(doorEntity: DoorEntity, state: Boolean){ 
    // Do something with the DoorEntity 
} 

這將涉及到我的表格單元格中看起來像這樣(請注意添加DoorEntity場)

import UIKit 

class SwitchTableCell: UITableViewCell { 
    @IBOutlet weak var title: UILabel! 
    @IBOutlet var cellSwitch: UISwitch! 
    var doorEntity: DoorEntity! 
    @IBAction func switchValueChanged(sender: UISwitch) { 
     print("Switch changed") 
     print("**") 
     print(self.title.text!) 
     print(self.cellSwitch.on) 
     print("**") 

    } 
} 

這裏查找實體交換機如何通過我的表模型添加。我無法弄清楚如何將DoorEntity傳遞給這裏的開關類:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("GarageDoorCell", forIndexPath: indexPath) as! SwitchTableCell 
     let door = self.doorEntityList[indexPath.row] as! DoorEntity 
     cell.textLabel?.text = door.name 
     print("Cell switch") 
     if(cell.cellSwitch == nil){ 
      print("Null switch") 
     } else { 
      print(cell.cellSwitch) 
     } 
     if(door.name == "Mike"){ 
      cell.cellSwitch?.on = false 
     } else { 
      cell.cellSwitch?.on = true; 
     } 
     return cell 
    } 

我想我可以對自定義的表格單元格中setDoorEntity,但似乎應該有一個更好的辦法。

有什麼建議嗎?

+0

這一切聽起來都很合理。你有什麼問題?什麼阻止你在單元格中存儲對'DoorEntity'的引用?您並不總是希望您的視圖對模型類具有直接依賴性,因爲這會降低可重用性等,但有時它只是最好的解決方案。如果你想真的做正確的事情,你可以定義一些單元知道的接口和'DoorEntity'可以實現的接口,從而消除直接的依賴關係。 – Caleb

+0

我想我是問是否合理,謝謝。我想我要做的是創建一個門服務對象,它與UI的東西無關。表單元格將獲得對該服務的引用,並且門服務將把命令發送給我擁有的Web服務。目前該視圖正在調用REST服務來獲取門的列表,所以服務也會這樣做,以解除耦合。 – mikeb

回答

0

只需添加委託方法?

protocol MainViewCellDelegate 
{ 
    func tableCellChange(doorEntity: DoorEntity, state: Boolean) 
} 

在你的表格單元格類:

import UIKit 

class SwitchTableCell: UITableViewCell 
{ 
    @IBOutlet weak var title: UILabel! 
    @IBOutlet var cellSwitch: UISwitch! 
    var doorEntity: DoorEntity! 

    var delegate: MainViewCellDelegate? = nil 

    @IBAction func switchValueChanged(sender: UISwitch) 
    { 
     if (delegate != nil) 
     { 
      delegate!.tableCellChange(doorEntity, state: cellSwitch.on) 
     } 
    } 
} 

在您的視圖控制器:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    let cell = tableView.dequeueReusableCellWithIdentifier("GarageDoorCell", forIndexPath: indexPath) as! SwitchTableCell 
    let door = self.doorEntityList[indexPath.row] as! DoorEntity 
    cell.textLabel?.text = door.name 
    cell.delegate = self 
    print("Cell switch") 
    if(cell.cellSwitch == nil) 
    { 
     print("Null switch") 
    } 
    else 
    { 
     print(cell.cellSwitch) 
    } 
    if(door.name == "Mike") 
    { 
     cell.cellSwitch?.on = false 
    } 
    else 
    { 
     cell.cellSwitch?.on = true; 
    } 
    return cell 
} 

func tableCellChange(doorEntity: DoorEntity, state: Boolean) 
{ 
    // Do something with the DoorEntity 
} 

不要忘了委託類MainViewCellDelegate添加到您的控制器類。

也許這有幫助嗎?

Greg