1

我正在尝试在 UIViewController 上显示自定义 TableView,但收到错误消息“UITableView dataSource 必须从 tableView:cellForRowAtIndexPath 返回一个单元格:”

我已将 TableView 连接到数据源和委托。

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

    static NSString *CellIdentifier = @"Custom";

    Custom *cell = (Custom *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {

        NSArray *topLevelObjects = [[NSBundle mainBundle]loadNibNamed:@"Custom" owner:self options:nil];

         for (id currentObject in topLevelObjects)
        {

            if ([currentObject isKindOfClass:[Custom class]])
            {
                cell = (Custom *) currentObject;
                 break;
            } 
        }  
     }

     cell.textLabel.text =[arr objectAtIndex:indexPath.row];

请帮忙

4

3 回答 3

2

在此处输入图像描述 在此处输入图像描述


#import <UIKit/UIKit.h>

@interface VocaTableCell : UITableViewCell 
{
    UILabel *vocaLabel;
}
@property (nonatomic, retain) IBOutlet UILabel *vocaLabel;

@end

#import "VocaTableCell.h"


@implementation VocaTableCell
@synthesize vocaLabel;

- (void)dealloc {
    [vocaLabel release];
    [super dealloc];
}
@end

你的代码有些错误。下面的代码请参考。我已经更正了。

下面的代码参考Befoe,您是否正确制作了customTableCell?下面的列表检查请。

有几种创建自定义表格单元的方法。

我的方式很受欢迎。

  1. 文件所有者的 CustomTabelCell 应该是NSObject。(非常重要的默认值是 Maybe UITableCell。你必须是更改文件的所有者 NSObject(NSObject 的意思是 id 类型!))

  2. CustomTableCell 将对象类链接到您的 CustomTableCell 类。

  3. 引用 Outlets 链接不是 File's Owner,必须是您的直接链接 CustomTableCell。


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

    static NSString *CellIdentifier = @"Custom";

    Custom *cell = (Custom *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell)
    {
        UINib *nib = [UINib nibWithNibName:@"Custom" bundle:nil];
        NSArray *arr = [nib instantiateWithOwner:nil options:nil];
        cell = [arr objectAtIndex:0];
    }

    cell.textLabel.text =[arr objectAtIndex:indexPath.row];
}
于 2011-10-10T13:34:55.727 回答
0

看起来您没有在方法结束时返回创建的单元格。添加'返回单元格;' 最后,错误将消失。

于 2011-10-10T13:12:54.913 回答
0

你添加了return语句吗?自定义是 UITableViewCell 的子类吗?

于 2011-10-10T13:12:59.127 回答