繁体   English   中英

UITableView数据源问题

[英]UITableView datasource issue

我正在尝试制作一个基本的自定义单元,并且在按照教程进行操作后,我遇到了这个问题。

reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

- (NSInteger)numberOfSections;
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array1 count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MainCell";

CustomCell *customCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

/*if (! customCell) {
    NSArray *parts = [[NSBundle mainBundle] loadNibNamed:@"cell" owner:nil options:nil];
    customCell = [parts objectAtIndex:0];
}
*/

customCell.lblTime.text = [array1 objectAtIndex:indexPath.row];
customCell.lblEvent.text = [array2 objectAtIndex:indexPath.row];

return customCell;
}

另外,我已经在tableView上设置了Identity,但是仍然无法正常工作,并且我正在使用Storyboard。 而且我确实链接了datasourcedelegate 请问我的问题在哪里?

您的自定义单元格是从UITableViewCell继承的吗? 您能告诉我们您的自定义单元代码吗? 您也可以尝试:

CustomCell *cell = [tableView dequeuReusableCellWithIdentifier:@"YourCellIdentifier" forIndexPath:indexPath];

使用dequeueReusableCellWithIdentifier:forIndexPath:而不是dequeueReusableCellWithIdentifier: dequeueReusableCellWithIdentifier:forIndexPath:如果您查看文档,

对于,

dequeueReusableCellWithIdentifier:forIndexPath

出于性能原因,表视图的数据源在为其表tableView:cellForRowAtIndexPath:方法中的行分配单元格时通常应重用UITableViewCell对象。 表视图维护数据源已标记为可重复使用的UITableViewCell对象的队列或列表。 当要求为表格视图提供新的单元格时,请从数据源对象中调用此方法。 如果有一个可用的单元格,此方法将使该单元出队,或者根据您先前注册的类或nib文件创建一个新的单元格。

对于dequeueReusableCellWithIdentifier:

出于性能原因,表视图的数据源在为其表tableView:cellForRowAtIndexPath:方法中的行分配单元格时通常应重用UITableViewCell对象。 表视图维护数据源已标记为可重复使用的UITableViewCell对象的队列或列表。 当要求为表格视图提供新的单元格时,请从数据源对象中调用此方法。 如果一个可用的单元格可用,则此方法使该单元出队,或者使用您先前注册的类或nib文件创建一个新的单元格。 如果没有可供重用的单元格,并且您没有注册类或nib文件,则此方法返回nil。

注意“此方法返回nil”。

仅供参考,在使用使用方法registerNib:forCellReuseIdentifier:之前,请不要忘记注册单元格Nib registerNib:forCellReuseIdentifier:

由于您正在使用情节提要,因此请确保情节提要中的“重用标识符”与用于创建单元格的那个相同。

第一个答案解决了您的错误:从情节提要中检查单元格标识符,它应在标识符名称下,而不是在还原ID下

第二个答案:您需要在填充cell.lblTime.text的地方放置一个断点,如果它为空,请检查填充数组的位置,并尝试理解为什么它为空。 请填写以询问其他问题。

在第一次执行时,单元格为nil。 因此,编写代码以初始化单元格。

-(UITableViewCell *)tableView : (UITableView *)tableView cellForRowAtIndexPath:  
   (NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"MainCell";

    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
       NSArray *parts = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
       cell = [parts objectAtIndex:0];
    }

    cell.lblTime.text = [array1 objectAtIndex:indexPath.row];
    cell.lblEvent.text = [array2 objectAtIndex:indexPath.row];

    return cell;
}

如果您已经在情节提要中创建了表格视图,并且在其中也有单元格,那么我认为您可能没有在情节提要中设置单元格标识符。

将故事板单元的重用标识符设置为“ MainCell”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM