首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift- UIViewController的自定义UITableViewCell委派只有一个协议有效

Swift- UIViewController的自定义UITableViewCell委派只有一个协议有效
EN

Stack Overflow用户
提问于 2016-09-14 08:29:33
回答 2查看 14.5K关注 0票数 5

在应用程序中,我有一些自定义协议,我的UIViewController遵循这些协议。我有一个自定义的tableViewCell类,里面有UIImageView和UITextView。出队后,我将单元的委托设置为UIViewController。但是,只有一个自定义协议进行回调(imagepicker协议)。

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
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()
   }
}

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

EN

回答 2

Stack Overflow用户

发布于 2016-09-14 08:37:53

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

代码语言:javascript
复制
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
}
票数 9
EN

Stack Overflow用户

发布于 2016-09-14 08:40:05

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

如果您同时设置了这两个属性,则您的实现应该可以工作。

代码语言:javascript
复制
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
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39480831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档