【问题标题】:Custom Uicells in UIviewController giving errorUIviewController 中的自定义 UIcells 给出错误
【发布时间】:2011-10-10 13:09:09
【问题描述】:
我试图在 UIViewController 上显示自定义 TableView,但收到错误“UITableView dataSource must return a cell from 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];
请帮忙
【问题讨论】:
标签:
iphone
uiviewcontroller
custom-cell
【解决方案1】:
#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?请检查以下列表。
关于自定义表格单元有几种创建方式。
我的举止很受欢迎。
文件所有者的CustomTabelCell 应该是一个NSObject。
(非常重要的默认是Maybe UITableCell。你必须是更改文件的所有者NSObject(NSObject 的意思是id 类型!))
CustomTableCell 将对象类链接到您的 CustomTableCell 类。
引用 Outlets 链接不是文件的所有者,必须是您的直接链接 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];
}
【解决方案2】:
看起来您没有在方法结束时返回创建的单元格。添加'返回单元格;'最后,错误将消失。
【解决方案3】:
你添加了return语句吗? Custom 是 UITableViewCell 的子类吗?