2016-09-14 16 views
3

在应用程序中,我有我的UIViewController符合的自定义协议。我有一个自定义tableViewCell类,并在那里有UIImageView和UITextView。出队后,我将单元的委托设置为UIViewController。然而,只有一个自定义协议会生成回调(imagepicker协议)。Swift自定义UITableViewCell委托给UIViewController只有一个协议工作

protocol customProtocol1{ 
    func pickImage(myInt: Int) 
} 
protocol customProtocol2{ 
    func protocol2 (myInt: Int) 
} 

class controller1: UIViewController, UITableViewDelegate, customProtocol1, customProtocol2 { 
    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section:Int) -> Int { 
     return 3 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomTableCellClass 
     cell.delegate = self 
     return cell 
    } 
    func pickImage (myInt: Int){ 
     print("This line prints") 
    } 

    func protocol2 (myInt: Int){ 
     print ("This line doesn't print") 


    } 
} 

而这里的customTableCellClass代码:

class CustomTableCellClass: UITableViewCell, UITextFieldDelegate, UITextViewDelegate { 
    var imageDelegate: customProtocol1? 
    @IBAction func pickImage(sender: AnyObject) { 
     imageDelagate?.pickImage(205) 
    } 

    var somethingElseDelegate: customProcotol2? 
    @IBActon func clickOnButton(sender: AnyObject) { 
     print("this line prints") 
     somethingElseDelegate?.protocol2(2) 
    } 

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

我的问题是,为什么第一个协议得到回调,但第二个不?

+0

您是不是指'cell.imageDelegate = self'? '代表'从哪里来? – Aerows

回答

6

从我在你的代码中看到,只设置了一个委托,在cellForRowAtIndexPath更改您的代码

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomTableCellClass 
    cell.imageDelegate = self 
    cell.somethingElseDelegate = self 
    return cell 
} 
+0

是的,我现在才明白这一点。谢谢回复! –

+0

@EugeneTemlock如果您认为它是正确答案,请接受我的回答:)谢谢 –

1

custom cell有两个委托性质imageDelegatesomethingElseDelegate,但在你的实现tableView(tableView:cellForRowAtIndexPath:)你只分配一个属性。

如果你设置了两个属性,你的实现应该可以工作。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    let cell = tableView.dequeReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomTableCellClass 
    cell.imageDelegate = self 
    cell.somethingElseDelegate = self 
    return cell 
}