一千萬個為什麽

搜索

UITableView設置獨立的委托對象?


I have setup a sample application using a UITableView. Initially I did this by conforming my controller to and , added a tableView in IB and connected "datasource" & "delegate" to Files Owner. It all works so thats good.

What I have been trying out is creating my own class for the delegate. I created a new class and added and , but quickly found I could not connect the tableView>dataSource/delegate. To solve this I added an "Object" (NSObject) in IB and set it to my new class. I then connected the dataSource and delegate outlets to this object. It sort of works, the app runs and displays the tableView, but when I try and scroll the table the app crashes. Can I ask if I am going about this the right way?

加裏

最佳答案

您正在做正確的事情,將您的類的實例放入nib文件中。我一直對NSTableView數據源使用該方法。你的問題是可能,你沒有正確地為一個筆尖中的對象初始化數據源/委托ivars。例如,如果在某些init方法中分配了NSArray,則可能無法調用init方法。您需要發布一些代碼來確認這一點。

無論如何,嘗試在新類中創建一個名為awakeFromNib的方法,並將初始化放入其中。

轉載註明原文: UITableView設置獨立的委托對象?

猜你喜歡