【问题标题】:Runtime Error when setting up a dataSource to UITableView将数据源设置为 UITableView 时出现运行时错误
【发布时间】:2015-02-13 14:26:06
【问题描述】:

所以我有这个控制器:

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!

}

然后我有数据源

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'

有什么线索吗?谢谢

【问题讨论】:

    标签: ios uitableview swift datasource


    【解决方案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]
        }
    
        ...
    }
    

    当然,这有点危险,因为您依赖调用者来确保他们请求正确的类型,但至少一旦调用者发出无效请求,它就会崩溃。

    【讨论】:

    • 我不能,因为超类是 NSObject 而 UITableViewDataSource 是协议
    • 啊,对,我习惯于在 Table View Controller 本身中处理这些东西。我会用另一个建议更新我的答案。
    • 此代码是否适用于类的非泛型版本(即public class MultipleSelectionsTable : NSObject, UITableViewDataSource)在此委托中使用泛型 swift 类型可能存在问题。
    • 它确实适用于非泛型类...所以我不能在那里使用泛型?
    • 在 Objective-c 代码和 obj-c isn't compatible with Swift generics 中的断言似乎失败了。此外,看起来泛型类的子类也必须是泛型的,所以看起来你也不能这样绕过它。不幸的是,看起来你被困在这里了。我建议将它作为一个错误报告给 Apple,并弄清楚如何在没有泛型类的情况下做到这一点。也许您可以创建一个通用 TableProvider 类并让您的代表将其作为成员持有?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    相关资源
    最近更新 更多