UITableView – 未调用cellForRowAtIndexPath
我知道这个标题已经足够说明了,我知道这个问题已被问过几次,但在这种情况下,我无法让它成功。
我已经使用过UITableViews并且它们都运行良好,但这一次,我甚至用其他工作副本检查了我的代码,但它无法正常工作。
所以,这是故事:
我在故事板中的一个视图控制器中有一个表视图,其中有一个名为ContactViewController
的自定义类,它使用名为favContactsTable
的sockets连接到代码
以下是.h和.m的相关部分:
ContactViewController.h
@interface ContactViewController : UIViewController // Other Stuff . . @property (retain, nonatomic) IBOutlet UITableView *favContactsTable; . . // Other Stuff @end
这里实现了UITableViewDataSource
协议的function:
ContactViewController.m
// Rest of the code . . . . - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 5; } - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"TEST OUTPUT"); //Test int a = 1; // Breakpoint a++; cell = [self.favContactsTable dequeueReusableCellWithIdentifier:@"FavContactCell"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FavContactCell"]; } cell.textLabel.text = @"FAV"; . . . }
我的视图控制器中有另一个按钮,用于重新加载此表的数据,但是当我按下它时,没有触发任何断点并且没有打印日志,也没有调用任何函数。 这是该按钮的代码:
- (IBAction)refreshTables:(id)sender { [self.favContactsTable reloadData]; }
我用我的其他项目检查了几乎每个部分都有这样的动态表视图,我无法弄清楚出了什么问题。
请不要提及其他类似的重复问题,因为我已经阅读了所有这些问题(或者至少是大部分问题)并且我确定我没有为每个部分返回0行,而且我确定favContactsTable
不是当我向它发送reloadData
消息并且……等时没有!
任何可能有用的想法都非常感激。
@interface myViewController : UIViewController { }
设置代表
拖动到您的viewcontroller图标
检查代码中的单元格标识符是否相同
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleTableCell"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"SimpleTableCell"]; } return cell; }
试试这段代码:
@interface ContactViewController : UIViewController . . .
然后在viewDidLoad
添加委托给你的tableview属性示例:
self.favContactsTable.delegate = self;
您没有确认这些协议: UITableViewDataSource
, UITableViewDelegate
。将此添加到您的文件中。
在那之后:
favContactsTable.delegate=self; favContactsTable.datasource=self;
并直接从您的Xib
文件中附加。