将数据源设置为UITableView时出现运行时错误

时间:2014-12-15 13:14:11

标签: ios uitableview swift datasource

所以我有这个控制器:

class OneIncidentController: IncidentController {

var number = 1;

let incident = Incident.getInstance()
var psyhicalsDataSource : MultipleSelectionsTable<Psyhical>?


@IBOutlet weak var psyhicalAggressionTable: UITableView!
...

override func viewDidLoad() {
    super.viewDidLoad()
    psyhicalsDataSource = MultipleSelectionsTable<Psyhical>(tableData: Incident.getInstance().psyhicals)
    psyhicalAggressionTable.dataSource = psyhicalsDataSource!

}

然后我有

的dataSource
import Foundation
import UIKit

public class MultipleSelectionsTable<T : AVObject> : NSObject, UITableViewDataSource {
var tableData : Array<T>?

init(tableData : Array<T>?) {
    self.tableData = tableData
    super.init()
}

public func tableView(tableView: UITableView, numberOfRowsInSection: Int) -> Int {
    if tableData != nil {
    return tableData!.count
    } else {
        return 0
    }
}

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

        var cell = tableView.dequeueReusableCellWithIdentifier("multipleSelectCell", forIndexPath: indexPath) as? MultipleSelectCell

        cell?.textLabel?.text = tableData![indexPath.row].name
        return cell!
    }

    deinit {
        println("Object was deinitialized")
    }
}

但在运行时我得到:

2014-12-15 15:07:40.183 AVSystem[3891:60b] -[_TtC8AVSystem23MultipleSelectionsTable0000000017DDCC0C tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x17dd9e90
2014-12-15 15:07:40.193 AVSystem[3891:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_TtC8AVSystem23MultipleSelectionsTable0000000017DDCC0C tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x17dd9e90'

有任何线索吗?感谢

1 个答案:

答案 0 :(得分:1)

看起来这是在Obj-C代码崩溃,当前的Obj-C和泛型相处不好。我向Apple提交了一个错误,并找到了从类中删除泛型参数的方法。

尝试这样的事情:

@objc protocol AVObject {

}

public class MultipleSelectionsTable : NSObject, UITableViewDataSource {

    private var tableData : Array<AnyObject>!

    func getTableData<T: AVObject >() -> Array<T> {
        return (tableData as? [T])!
    }

    func setTableData<T: AVObject>(x: Array<T>) -> Void {
        tableData = x as [AnyObject]
    }

    ...
}

当然,这有点危险,因为你依赖你的呼叫者来确保他们要求正确的类型,但至少它会在呼叫者发出无效请求时立即崩溃。