ios UITableView - cellForRowAtIndexPath 未被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21794262/
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
UITableView - cellForRowAtIndexPath Is Not Being Called
提问by Sepehrom
I know the title is illustrating enough, and I KNOW that this question has been ask a couple of times, but in this case I couldn't manage to make it work.
我知道标题已经足够说明问题了,我知道这个问题已经被问过几次了,但在这种情况下,我无法让它发挥作用。
I already have worked with UITableViews and they all have been working fine, but this time, I even checked my code with other working copies, but it is not working.
我已经使用过 UITableViews 并且它们都运行良好,但是这一次,我什至用其他工作副本检查了我的代码,但它不起作用。
So, here's the story :
所以,这是故事:
I have a table view in one of my view controller's in storyboard with a custom class named ContactViewController
, and it is connected to code using an outlet named favContactsTable
我在情节提要中的一个视图控制器中有一个表视图,其中有一个名为 的自定义类ContactViewController
,它使用名为 的插座连接到代码favContactsTable
Here are related parts from its .h and .m :
以下是 .h 和 .m 的相关部分:
ContactViewController.h
ContactViewController.h
@interface ContactViewController : UIViewController <UITableViewDataSource>
// Other Stuff
.
.
@property (retain, nonatomic) IBOutlet UITableView *favContactsTable;
.
.
// Other Stuff
@end
And here is implemented functions of UITableViewDataSource
protocol :
这是UITableViewDataSource
协议的实现功能:
ContactViewController.m
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";
.
.
.
}
I have another button in my view controller that reloads data for this table, but when I press it none of the breakpoints is triggered and no log is printed, none of the functions are being called. Here's the code for that button :
我的视图控制器中有另一个按钮可以重新加载该表的数据,但是当我按下它时,没有触发任何断点,也没有打印日志,也没有调用任何函数。这是该按钮的代码:
- (IBAction)refreshTables:(id)sender
{
[self.favContactsTable reloadData];
}
I checked almost every part with my other projects that has a dynamic table view like this, and I can't figure out what's wrong.
我检查了我的其他项目的几乎所有部分,这些项目具有像这样的动态表视图,但我无法弄清楚出了什么问题。
Please do not mention other similar questions as duplicates, because I already have read them all ( or at least, most of them ) and I'm sure I'm not returning 0 rows for each section, and I'm sure favContactsTable
is not nil
when I send reloadData
message to it and ... etc !
请不要说其他类似的问题为重复,因为我已经有阅读所有(或至少大多数),我敢肯定,我不返回0行的每个部分,我敢肯定,favContactsTable
是不是nil
当我向reloadData
它发送消息......等等!
Any idea that may help is really appreciated.
任何可能有帮助的想法都非常感谢。
回答by codercat
@interface myViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
}
set delegate
设置委托
drag to your viewcontroller icon
拖动到您的视图控制器图标
check your cell identifier as same in your code also
检查您的单元格标识符是否与您的代码相同
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleTableCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:@"SimpleTableCell"];
}
return cell;
}
回答by Robert
try this code :
试试这个代码:
@interface ContactViewController : UIViewController <UITableViewDataSource, UITableViewDelegate >
.
.
.
and then in viewDidLoad
add delegate to your tableview property example :
然后viewDidLoad
将委托添加到您的 tableview 属性示例中:
self.favContactsTable.delegate = self;
回答by Charlie
You did not confirmed these protocols :UITableViewDataSource
, UITableViewDelegate
.Add this to your file.
您没有确认这些协议:UITableViewDataSource
,UITableViewDelegate
。将其添加到您的文件中。
and after that:
在那之后:
favContactsTable.delegate=self;
favContactsTable.datasource=self;
and attach directly from your Xib
file.
并直接从您的Xib
文件中附加。