简体   繁体   English

在Swift中使用协议和委托将数据从UITableViewDataSource传递到UIViewController

[英]Passing data from UITableViewDataSource to UIViewController using protocol and delegate in Swift

I'm trying to pass data from a UITableViewDataSource to a UIViewController when a table row is selected. 当尝试选择表行时,我试图将数据从UITableViewDataSource传递到UIViewController。 I think I'm pretty close. 我想我很近了。

Protocol 协议

protocol BusDataDelegate {
    func didSelectRow(row: NSIndexPath, data: Route)
}

Data Source 数据源

class BusUITableView: NSObject, UITableViewDelegate, UITableViewDataSource {

    var busDataDelegate: BusDataDelegate?

    var routeService: RouteService = RouteService()

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

        var busRoutes: [Route] = routeService.retrieve()
        return busRoutes.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell:BusUITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as BusUITableViewCell

        var busRoutes: [Route] = routeService.retrieve()

        cell.routeName.text = busRoutes[indexPath.row].name
        cell.routeNumber.text = busRoutes[indexPath.row].id
        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        var busRoutes: [Route] = routeService.retrieve()

        busDataDelegate?.didSelectRow(indexPath, data: busRoutes[indexPath.row])
        println("You selected cell #\(indexPath.row)!")

    }

}

View Controller - abbreviated View Controller-缩写

class ViewController: UIViewController, MKMapViewDelegate, BusDataDelegate {

    func didSelectRow(row: NSIndexPath, data: Route) {
        println("Row: \(row) Data: \(data)")
    }

}

The issue is you only declared the busDataDelegate object, not assigned anything to it or not allocated it. 问题是您只声明了busDataDelegate对象,没有busDataDelegate分配任何内容或未分配任何内容。

You are declaring it like: 您正在这样声明:

var busDataDelegate: BusDataDelegate?

And using it like: 并像这样使用它:

busDataDelegate?.didSelectRow(indexPath, data: busRoutes[indexPath.row])

But never assigned anything to that object or allocated it, that's the problem. 但是从来没有为该对象分配任何东西或分配它,这就是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 协议委托未按预期传递数据 [Swift5] - Protocol Delegate not passing data as expected [Swift5] 如何在Swift中使用Protocol和Delegate将变量从UIViewController传递回根导航控制器 - how to pass a variable back to root navigation controller from the UIViewController using Protocol and Delegate in Swift iOS - 使用协议和委托将数据从 Swift 传递到 ObjC VC - iOS - Pass Data from Swift to ObjC VC using protocol and delegate Swift 2:UITableViewDataSource协议扩展 - Swift 2: UITableViewDataSource protocol extension 问题将数据传递回第一个视图 controller 与委托和协议(Swift) - issue passing data back to first view controller with delegate and protocol (Swift) 将数据从tableView传递到UIViewController swift 3 - Passing data from tableView to UIViewController swift 3 在 Swift 5 中将数据从 UIPickerView 传递到 UIViewController - Passing data from a UIPickerView to a UIViewController in Swift 5 Swift-使用协议和委托将值传递回ViewController - Swift - Passing value back to ViewController using protocol and delegate 我的UIViewController“不符合协议:'UITableViewDataSource'” - My UIViewController “does not conform to protocol: 'UITableViewDataSource'” 协议要求委托从UIViewController继承 - Protocol requires that delegate inherit from UIViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM