我正在显示通过 Storyboard 初始化的动态表格 View 。
没什么特别的,只是用文本标签初始化 3 个单元格。
以下是实际发生的情况:
2014-08-15 12:39:19.909 PopTestAnimation[31825:60b] numberOfSectionsInTableView
2014-08-15 12:39:19.926 PopTestAnimation[31825:60b] numberOfSectionsInTableView
2014-08-15 12:39:19.927 PopTestAnimation[31825:60b] numberOfRowsInSection
2014-08-15 12:39:19.929 PopTestAnimation[31825:60b] cellForRowAtIndexPath: 0
2014-08-15 12:39:19.943 PopTestAnimation[31825:60b] Displaying row: 0
2014-08-15 12:39:19.943 PopTestAnimation[31825:60b] cellForRowAtIndexPath: 1
2014-08-15 12:39:19.945 PopTestAnimation[31825:60b] Displaying row: 1
2014-08-15 12:39:19.945 PopTestAnimation[31825:60b] cellForRowAtIndexPath: 2
2014-08-15 12:39:19.946 PopTestAnimation[31825:60b] Displaying row: 2
2014-08-15 12:39:19.947 PopTestAnimation[31825:60b] numberOfSectionsInTableView
2014-08-15 12:39:19.947 PopTestAnimation[31825:60b] numberOfRowsInSection
2014-08-15 12:39:19.948 PopTestAnimation[31825:60b] cellForRowAtIndexPath: 0
2014-08-15 12:39:19.949 PopTestAnimation[31825:60b] Displaying row: 0
2014-08-15 12:39:19.949 PopTestAnimation[31825:60b] cellForRowAtIndexPath: 1
2014-08-15 12:39:19.950 PopTestAnimation[31825:60b] Displaying row: 1
2014-08-15 12:39:19.951 PopTestAnimation[31825:60b] cellForRowAtIndexPath: 2
2014-08-15 12:39:19.952 PopTestAnimation[31825:60b] Displaying row: 2
由于某种原因,似乎每个单元格都被初始化了两次。任何想法为什么?
这里是 code
最佳答案
我刚刚检查了 Storyboard文件。在 TraffleViewController(UITableViewController 的子类)的 Storyboard中,您有一个 UITableViewWrapper 和另一个 UITableView。
里面应该只有一个 UITableViewWrapper 而没有其他 UITableView。由于 UITableViewWrapper 中有一个 UITableView ,因此有两个调用:
cellForRowAtIndexPath:
如果您进行调试并在其中打印 UITableView 的实例:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
你会发现两个独立的 UITableView 实例。请按照以下步骤解决。
1.Remove the view called "Table View" from storyboard.
2.Drag a UITableView inside TraffleViewController
3.Set the datasource and delegate of UITableView to TraffleViewController.
关于ios - cellForRowAtIndexPath 被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25333181/