Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

ios - Swift- custom UITableViewCell delegate to UIViewController only one protocol works

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")


   }
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...