如何创建一个自定义委托方法,如tableView的cellForRowAtIndexPath方法,该方法有一个tableView的争论?

时间:2016-04-03 12:02:09

标签: ios objective-c swift uitableview delegates

如何使用自定义视图的委托方法创建自定义委托,并将其(自定义视图)对象作为该委托方法中的参数,就像tableView的cellForRowAtIndexPath:方法一样?

使用确切问题详细阐述问题:

到目前为止,我一直使用UIKit中提供的不同视图的委托方法在我的应用程序中构建视图。但是这次我需要使用自定义委托和数据源创建自定义View。我已经做到了。但一个问题总是会提升。也就是说,每当我在ViewController中需要多个相同的自定义视图时,我将如何在我的委托方法中单独识别它们。当然,我需要在我的委托方法中传递自定义View的对象作为争论。例如。

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath;

在上面的委托方法中,tableView对象是一个参数,使用它可以识别tableView,如果一个ViewController上有多个tableViews,并且这个View Controller被设置为所有这些tableViews的委托。

这是真实的情景:

  //
  //  ViewController.swift
  //  DemoCustomViewDelegate
  //
  //  Created by Shubham Ojha on 4/3/16.
  //  Copyright © 2016 Shubham Ojha. All rights reserved.
  //

  import UIKit

  protocol MyCustomViewDelegate {
  func myCustomView(myCustomView: MyCustomView, didSomethingWithAString aString: String)
  }

  class MyCustomView: UIView {
  var delegate: AnyObject?
  init(frame: CGRect, andViewId viewId: String, andDelegate delegate: AnyObject) {
    super.init(frame: frame)
    self.delegate = delegate
    self.doSomethingWithAString(viewId)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
}
func doSomethingWithAString(aString: String) {
    // do something with a string


        delegate!.myCustomView(self, didSomethingWithAString: aString)

}
}
 class ViewController: UIViewController {

var myView: MyCustomView!
override func viewDidLoad() {
    super.viewDidLoad()

    myView = MyCustomView.init(frame: CGRectMake(0, 0, 50, 50), andViewId: "100", andDelegate: self)
    //set the delegate
    myView.delegate = self

}
func myCustomView(myCustomView: MyCustomView, didSomethingWithAString aString: String) {
    //this delegate method is called but the object of myCustomView I need could not get
    if(myCustomView.isEqual(myView)){
        print("Got the object")//**Does not prints, this is what I actually need to print...**

    }
}

}

您可以在ViewController中复制粘贴代码并评估问题。

欢迎快速回答!

4 个答案:

答案 0 :(得分:2)

这是你的意思吗?

protocol MyCustomViewDelegate {
    func myCustomView(myCustomView: MyCustomView, didSomethingWithAString aString: String)
}

class MyCustomView: UIView {
    var delegate: MyCustomViewDelegate?

    func doSomethingWithAString(aString: String) {
        // do something with a string

        // inform the delegate
        delegate?.myCustomView(self, didSomethingWithAString: aString)
    }
}

答案 1 :(得分:1)

您发布的代码是正确的。 在您的ViewController中执行类似

的操作时
let myView = MyCustomView()
myView.delegate = self

您告诉MyCustomView对象将self称为delegate

因此,当MyCustomView执行此代码时

delegate!.myCustomView(self, didSomethingWithAString: aString)

它在你的代码中调用它

func myCustomView(myCustomView: MyCustomView, didSomethingWithAString aString: String) {
    //this delegate method is called but the object of myCustomView I need could not get
    if(myCustomView.isEqual(myView)){
        print("Got the object")//**Does not prints, this is what I actually need to print...**

     }
}

其中myCustomView等于myView。您可以使用==运算符代替isEqual来比较它们。

答案 2 :(得分:0)

您可以查找以下扩展程序:

    extension ViewController: UITableViewDelegate {
// You extension to the UITableViewDelegate class
}

答案 3 :(得分:0)

protocol MyCustomViewDelegate {
    func myCustomView(myCustomView: MyCustomView, didSomethingWithAString aString: String)
 }

class MyCustomView: UIView {
    var delegate: MyCustomViewDelegate?

    func doSomethingWithAString(aString: String) {
    // do something with a string

        if let delegate = delegate {
            delegate.myCustomView(self, didSomethingWithAString: aString)
        }
    }
}


class ViewController: UIVIewController {

    var myView: MyCustomView!
    override func viewDidLoad() {
        super.viewDidLoad()
        myView = MyCustomView()

        //set the delegate
        myView.delegate = self

    }
}

//implement the delegate
extension ViewController: MyCustomViewDelegate {
    func myCustomView(myCustomView: MyCustomView, didSomethingWithAString aString: String) {
        //get the string from delegation
    }
}