我正在尝试使用超类填充并处理某个段的表的内容。所以我想到实施:在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”应该执行父母的执行? –