xcode TableView 与协议 UITableViewDataSource 和 Xib 文件的冗余一致性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34099847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redundant conformance of TableView to protocol UITableViewDataSource with Xib Files
提问by senty
I have a parent view UIViewController (on storyboard), a TableViewController with .xib and TableViewCell with .xib. I am trying to connect DataSource to the TableView however it's giving me an error:
我有一个父视图 UIViewController(在故事板上),一个带有 .xib 的 TableViewController 和带有 .xib 的 TableViewCell。我正在尝试将 DataSource 连接到 TableView 但是它给了我一个错误:
Redundant conformance of 'TableView1' to protocol 'UITableViewDataSource'
'TableView1' inherits conformance to protocol 'UITableViewDataSource' from superclass here.
“TableView1”对协议“UITableViewDataSource”的冗余一致性
'TableView1' 从这里的超类继承了对协议 'UITableViewDataSource' 的一致性。
Without adding dataSource near class and try it as class TableView1: UITableViewController {..
, it doesn't give me any error and in the simulator, I can see the table view illusion when I scroll down.
没有在类附近添加 dataSource 并将其作为 尝试class TableView1: UITableViewController {..
,它不会给我任何错误,并且在模拟器中,当我向下滚动时,我可以看到表视图错觉。
However, when I try to add dataSource, it gave me these errors.
但是,当我尝试添加数据源时,它给了我这些错误。
The path I followed on setting it up...:
我在设置时遵循的路径...:
Ctrl + drag from xib to TableView1 and connected it as
Globals
In xib file, I connected DataSource & Delegate
Ctrl + 从 xib 拖动到 TableView1 并将其连接为
Globals
在 xib 文件中,我连接了 DataSource & Delegate
- Finally, my TableView1:
- 最后,我的 TableView1:
class TableView1: UITableViewController, UITableViewDataSource {
error here..
class TableView1: UITableViewController, UITableViewDataSource {
错误在这里..
@IBOutlet var GlobalsTableView: UITableView!
var results: [AnyObject]? = []
override func viewDidLoad() {
super.viewDidLoad()
print("A")
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return self.results?.count ?? 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! DogTableViewCell
return cell
}
}
Please note that in TableView1.xib, I can't select TableView1
as Custom Class -> Class (but i don't think it's necessary).
请注意,在 TableView1.xib 中,我不能选择TableView1
自定义类 -> 类(但我认为没有必要)。
回答by R P
When a class inherits from UITableViewController
, it by default conforms to UITableViewDataSource
& UITableViewDelegate
and you need not explicitly specify it.
当一个类继承自 时UITableViewController
,它默认符合UITableViewDataSource
&UITableViewDelegate
并且您不需要显式指定它。
You need to conform to UITableViewDataSource
and UITableViewDelegate
only when you embed a UITableView
in a UIViewController
.
您需要符合UITableViewDataSource
且UITableViewDelegate
仅当您将 a 嵌入UITableView
到UIViewController
.
回答by Onat Korucu
There are at least 2 conformations in your class. You need to extend only once.
你的班级至少有 2 个构象。您只需扩展一次。
First Scenario:
第一个场景:
You conform in the class description AND in the extension.
您符合类描述和扩展。
class MyViewController: MyDelegate{
//class functions here
}
extension MyViewController: MyDelegate{
func1()
}
Remove "My Delegate" in the class description.
删除课程描述中的“我的代表”。
class MyViewController{
//class functions here
}
extension MyViewController: MyDelegate{
func1()
}
Second Scenario:
第二个场景:
You conform in two extensions.
你符合两个扩展。
extension MyViewController: MyDelegate{
func1()
}
extension MyViewController: MyDelegate{
func2()
}
Merge them into one extension like:
将它们合并为一个扩展,例如:
extension MyViewController: MyDelegate{
func1()
func2()
}