我正在嘗試使用超類填充並處理某個段的表的內容。所以我想到實施:在Swift中將UITableView委託和數據源分配給super
@IBAction func changeValue(sender:AnyObject){
self.searchDisplayController?.setActive(false, animated:false)
if (selection.selectedSegmentIndex==1){
self.myTableView.delegate=super
self.myTableView.dataSource=super
} else {
self.myTableView.delegate=self
self.myTableView.dataSource=self
}
self.myTableView.reloadData()
}
但我有一個錯誤。通過測試的方式,編譯器建議我使用:
@IBAction func changeValue(sender:AnyObject){
self.searchDisplayController?.setActive(false, animated:false)
if (selection.selectedSegmentIndex==1){
self.myTableView.delegate=super.self()
self.myTableView.dataSource=super.self()
} else {
self.myTableView.delegate=self
self.myTableView.dataSource=self
}
self.myTableView.reloadData()
}
任何結構super.self()的含義
然而,儘管該代碼通過有通過沒有任何問題,命令似乎被忽略,委託方法被調用在相同的類而不是超類,甚至打印super.self()的值顯示它是當前類,當然,當我跳過()時,Xcode鼓勵我說:
函數產生預期的類型'LogsViewController';你的意思是用'()'來稱呼它嗎?
但是,當我添加雙括號時,它返回當前類而不是頂部LogsViewController。 什麼是實現我需要的正確方式,以及爲什麼super.self()不能像廣告一樣工作?
我澄清我不能簡單地調用超級代理方法,因爲它應該保留底層類的所有權,並且頂級類的虛擬方法應該在底層類中找到方法,而不是超類的方法,因爲它將需要。
那我的代碼的結構,希望它使事情更清楚: 中底層
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
let isMainTable = tableView==self.myTableView
let isFavorite = selection.selectedSegmentIndex==2
var count: Int?
count = sourceForTableKind(isMainTable, favorites:isFavorite)?.count
if (count==nil) {
return 0
}
return count!
}
評出的 「虛類」:
func sourceArray()->Array<NearElement>?{
return nil
}
func arrayOfContentsFromSearchArray(searchArray:Array<String>?, favorites:Bool)->Array<NearElement>?{
return nil
}
func sourceForTableKind(normal: Bool, favorites:Bool)->Array<NearElement>?{
// NSLog(@"source:%@ %@",[email protected]"favorites": @"near", [email protected]"normal":@"searched");
print("sono \(self)")
if (normal) {
return sourceArray()
} else {
return arrayOfContentsFromSearchArray(searchResults, favorites:favorites)
}
}
sourceArray的實現( )和arrayOfContentsFromSearchArray()都在底層類和它的超類中定義,它們都從這個類繼承。
你需要那個「sourceForTableKind」應該執行父母的執行? –