我有一个独立于 UIViewController 的类,设置为我的 UITableView 委托(delegate)和我的 UITableViewDataSource。
我尝试初始化 UITableViewDelegate 类,然后将其分配给 UITableView。
奇怪的是...
方法 numberOfSectionsInTableView 和 tableView(:numberOfRowsInSection) 被调用了五次,而 tableView(_:cellForRowAtIndexPath) 从未被调用。
我已经验证了 numberOfSectionsInTableView 和 tableView(:numberOfRowsInSection) 至少返回一个值。
如果我将 UITableViewDataSource 和 UITableViewDelegate 方法移至 ViewController,代码将正常工作。
是什么导致了这种行为?
class MessagesViewController: UIViewController, ManagedObjectContextSettable {
var managedObjectContext: NSManagedObjectContext!
@IBOutlet var messagesTableView: UITableView!
override func viewDidLoad() {
setupMessagesTableView()
}
private func setupMessagesTableView() {
let dataSource = MessagesTableViewDataSource(managedObjectContext: managedObjectContext, conversationList: fetchedObjects as! [Conversation])
// Assume fetchedObjects is an array fetched from CoreData store. I have removed the code that defines it for the purpose of this example.
self.messagesTableView.dataSource = dataSource
self.messagesTableView.delegate = dataSource
}
}
class MessagesTableViewDataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
var managedObjectContext: NSManagedObjectContext!
var conversationList: [Conversation]
required init(managedObjectContext: NSManagedObjectContext, conversationList: [Conversation]) {
self.managedObjectContext = managedObjectContext
self.conversationList = conversationList
let conversation = Conversation()
self.conversationList.append(conversation)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.conversationList.count }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CellID", forIndexPath: indexPath) as UITableViewCell!
let conversation: Conversation = self.conversationList[indexPath.row]
cell.textLabel!.text = conversation.name as? String
return cell
}
}
最佳答案
问题是您的 MessagesTableViewDataSource 实例被释放。 UITableView 的 delegate 和 dataSource 属性很弱。您将数据源 (MessagesTableViewDataSource) 声明为函数内的局部变量,因此没有任何内容持有对 MessagesTableViewDataSource 实例的强引用。
要解决此问题,请为 dataSource 定义一个实例变量并将其分配给 viewDidLoad。
示例代码:
类 MessagesViewController: UIViewController, ManagedObjectContextSettable {
let dataSource: MessagesTableViewDataSource?
var managedObjectContext: NSManagedObjectContext!
@IBOutlet var messagesTableView: UITableView!
override func viewDidLoad() {
setupMessagesTableView()
}
private func setupMessagesTableView() {
dataSource = MessagesTableViewDataSource(managedObjectContext: managedObjectContext, conversationList: fetchedObjects as! [Conversation])
// Assume fetchedObjects is an array fetched from CoreData store. I have removed the code that defines it for the purpose of this example.
self.messagesTableView?.dataSource = dataSource
self.messagesTableView?.delegate = dataSource
}
关于swift - 一些 UITableView Delegate 方法被调用了 5 次,其他的则根本没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35354929/