Swift-自定义 UITableViewCell 委托给 UIViewController 只有一种协议有效 [英] Swift- custom UITableViewCell delegate to UIViewController only one protocol works

查看:14
本文介绍了Swift-自定义 UITableViewCell 委托给 UIViewController 只有一种协议有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用程序中,我有我的 UIViewController 符合的自定义协议.我有一个自定义 tableViewCell 类,其中有 UIImageView 和 UITextView.出队后,我将单元格的委托设置为 UIViewController.但是,只有一种自定义协议进行回调(imagepicker 协议).

In the application, I have custom protocols that my UIViewController conforms to. I have a custom tableViewCell class and have UIImageView and UITextView in there. I set the cell's delegate to the UIViewController after dequeuing. However only one of the custom protocols makes the callback (imagepicker protocol).

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 代码:

And here's the customTableCellClass code:

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()
   }
}

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

My question is, why does the first protocol get callbacks but second does not?

推荐答案

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

From what I see in your code, you only set one delegate, change your code in cellForRowAtIndexPath to

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
}

这篇关于Swift-自定义 UITableViewCell 委托给 UIViewController 只有一种协议有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆