Ios 我在UITableViewController中覆盖了什么?
许多关于UITableView的教程要求我们在Ios 我在UITableViewController中覆盖了什么?,ios,uitableview,swift,Ios,Uitableview,Swift,许多关于UITableView的教程要求我们在MyUITableViewController:UITableViewController中重写此函数: override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int { return 10 } 我知道这是UITableView中numberOfRowsInSection方法的实现,但我不理解语言机制 tableV
MyUITableViewController:UITableViewController
中重写此函数:
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
return 10
}
我知道这是UITableView
中numberOfRowsInSection方法的实现,但我不理解语言机制
tableView
是UITableViewController
的继承属性,而不是方法。那么重写究竟是如何工作的呢?同一方法的objective-C语法是
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section;
它给出了tableView:numberOfRowsInSection:
当你习惯于使用Objective-C时,你会在心理上忽略表格视图:
部分,而专注于行数部分
当Objective-C方法名称转换为swift时,第一个参数将成为方法名称,因此您将获得tableView(tableView:UITableView?,numberOfRowsInSection:Int)->Int
对您的问题的简短回答是,您正在使用该签名覆盖函数,而不是
tableView
属性UITableViewController
符合UITableViewDataSource
协议:
tableView
是在UITableViewDataSource
UITableViewController
有一个协议函数的默认实现,MyUITableViewController
可以override
这只是swift帮助符合Objective-C详细函数名的愚蠢方式。我相信tableView实际上是一个函数,它有几个不同的参数集,这些参数反过来定义了几个不同的函数。您只是用一组特定的参数覆盖函数。@TravisM。这是正确的。在ObjC中,方法名相当于函数名和所有命名参数的串联。但是tableView
不是方法。在UITableViewController
内部,您可以看到var tableView:UITableView代码>