![](/img/trans.png)
[英]UITableView tableView:cellForRowAtIndexPath: not getting called
[英]-tableView:cellForRowAtIndexPath: is not getting called
我在UIViewController
有UITableView
,我通过outlet和delegete连接,数据源被分配给self。 numberOfSectionsInTableView
和numberOfRowsInSection
正在调用我试过使用NSLog
但是cellForRowAtIndexPath
没有得到调用我的代码看起来像
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@" cellForRowAtIndexPath ");
static NSString *CellIdentifier = @"CurrencyCell";
CurrencyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CurrencyCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
}
new *Obj = [appdelegate.array objectAtIndex:indexPath.row];
cell.CurNameLabel.text = Obj.C_Name;
cell.textLabel.text = @"sdfnhsdfndsio;";
NSLog(@"C_Name %@", Obj.C_Name);
return cell;
}
任何一个电话我都会犯错误提前谢谢
似乎返回的numberOfSectionsInTableView
和numberOfRowsInSection
是0.如果是这种情况,那么将不会调用cellForRowAtIndexPath。
从上面的注释..似乎“YourAppDelegate”变量为null
或yourArray为null
尝试在这些委托上保留断点,以检查它们返回的值
尝试在.h文件中添加委托和数据源,如下所示。
@interface AddProjectViewController : UIViewController<
UITableViewDataSource,UITableViewDelegate>
.....
@end
在viewDidLoad:
把这段代码..
- (void)viewDidLoad
{
yourTableView.delegate= self;
yourTableView.dataSource=self;
}
最后在numberOfRowsInSection
方法中尝试这个,返回一些大于0的int
值
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [yourArray count];//or any int value
}
似乎返回的numberOfSectionsInTableView
和numberOfRowsInSection
是0或者可能是另一个原因
仔细检查以下几点
1如果要从XIB创建TableView,则应设置数据源并委托给文件的所有者。
如果你是编程创建它,那么你应该设置委托和数据源,如下,不要忘记采取dataSourec
并delegate
了UItableView
在.h
YourViewController : UIViewController<UITableViewdelegate, UITableViewDatasource>
将下面的代码行放在创建UITableView的行之后
tableViewObj.delegate= self;
tableViewObj.dataSource=self
2仔细检查您的datasource
(无论Array的名称)是否有数据,因为每当您创建传递数组的行数时,计数如下所示
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int numberOfRows= [datasource count];
return numberOfRows;
// here check does the `numberOfRows` have nonzero value or just 0
if it retunes 0 then cellForAtIndexPath would not call .
}
我希望它对你有帮助
如果在应用程序中启用了多线程概念,即使在tableview
上调用reloadData
方法,有时也不会调用cellForRowAtIndexPath
。 所以调用这样的realod方法:
[self performSelectorOnMainThread:@selector(reloadTable) withObject:self waitUntilDone:NO];
它可能会奏效。
- (void)reloadTable
{
[self.tableView reloadData];
}
如果您不在多线程环境中,那么您需要确保tableviews
delegate
和datasource
不为空。
使用“numberOfRowsInSection”方法检查数组中的数字值。 在返回值之前打印您的数组。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"%@",returnedArray);
return [returnedArray count];//
}
您也可以尝试在方法中发送一些静态行数来检查。
看起来你的数组在该方法中变为null。
希望它能解决你的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.