2016-09-08 3 views

Antwort

0

Ich habe dies getan, um eine lange ViewController mit einem UIPickerViewDelegate/DS zu vermeiden. Sie können einfach eine Klasse erstellen, die UITableViewDelegate und UITableViewDataSource entspricht, dieses Objekt in Ihrem View-Controller instanziieren und es als dataSource und Delegate der Tabellenansicht zuweisen. Damit diese Klasse Daten an Ihren ViewController zurücksendet, müssen Sie ein Protokoll für die VC erstellen, damit sie sich an die Klasse anpasst und der Klasse auch einen Delegaten geben.

Ich lese, dass diese Klasse von NSObject erben muss, da die Protokolle NSObject-Protokolle sind, und es wirft und Fehler, wenn sie nicht sind.

class MyCustomTableViewDel: NSObject, UITableViewDataSource, UITableViewDelegate { 

    weak var secondaryDelegate: TableViewSecondaryDelegate? 
    let rowData: [String] 
    init(dataForRows: [String]) { 
     rowData = dataForRows 
     super.init() 
    } 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 


    } 

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

     return rowData.count 

    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     secondaryDelegate?.doSomething(indexPath.row) 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     .... 
     return SomeCellForTheTableView 
    } 

} 

dann das sekundäre Protokoll machen:

protocol TableViewSecondaryDelegate { 
    func doSomething(row: Int) 
} 

dann in Ihrem Viewcontroller:

class myTableViewSceneController: UIViewController, TableViewSecondaryDelegate { 
    override func viewDidLoad() { 
     .... 
     let tableViewDelAndDS = MyCustomTableViewDel(dataForRows: ["row0", "row1"]) 
     tableViewDelAndDS.secondaryDelegate = self 
     tableView.delegate = tableViewDelAndDS 
     tableView.dataSource = tableViewDelAndDS 
    } 
     func doSomething(row: Int) { ... } 
} 
2

Sie benötigen eine andere Klasse für den Zweck zu schaffen, sondern erst mal sehen, wie der Viewcontroller wird sein.

Wie Sie den Code sehen Art selbsterklärend ist, ich habe eine benutzerdefinierte Klasse erstellt TableViewDelegate genannt, die als Delegierten und Datasource des Tableview eingestellt werden.

Wir geben zu TableViewDelegate die Daten, die in der tableview gezeigt werden, und FunktiondidSelectRow genannt, die durch TableViewDelegate aufgerufen wird, sobald eine Zeile ausgewählt ist.

class ViewController: UIViewController { 

    @IBOutlet weak var tableView: UITableView! 

    // data source 
    var data = [1, 2, 3, 4] 

    // delegate 
    var tableViewDelegate: TableViewDelegate? 

    override func viewDidLoad() { 
    super.viewDidLoad() 

    // creating the delegate object and passing the data 
    tableViewDelegate = TableViewDelegate(data: data) 

    // passing a function to the delegate object 
    tableViewDelegate?.didSelectRow = didSelectRow 

    // setting the delegate object to tableView 
    tableView.delegate = tableViewDelegate 
    tableView.dataSource = tableViewDelegate 
    } 

    // a function that will be called by the delegate object 
    // when a row is selected 
    func didSelectRow(dataItem: Int, cell: UITableViewCell) { 
    let alert = UIAlertController(title: "Info", message: "\(dataItem) was selected.", preferredStyle: .Alert) 
    alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
    presentViewController(alert, animated: true, completion: nil) 
    } 
} 

Die TableViewDelegate die UITableViewDelegate Zusammenhang von in Rechnung von allem ist und UITableViewDataSource Protokolle.

class TableViewDelegate: NSObject, UITableViewDelegate, UITableViewDataSource { 
    var data = [Int]() 

    // variable that holds a stores a function 
    // which return Void but accept an Int and a UITableViewCell as arguments. 
    var didSelectRow: ((dataItem: Int, cell: UITableViewCell) -> Void)? 

    init(data: [Int]) { 
    self.data = data 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return data.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) 

    let text = String(data[indexPath.row]) 
    cell.textLabel?.text = text 

    return cell 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let cell = tableView.cellForRowAtIndexPath(indexPath)! 
    let dataItem = data[indexPath.row] 

    if let didSelectRow = didSelectRow { 
     // Calling didSelectRow that was set in ViewController. 
     didSelectRow(dataItem: dataItem, cell: cell) 
    } 
    } 
} 

Ergebnis:

Verwandte Themen