Iphone 无法在UITableView的自定义单元格中加载数据
请帮助我使用UITableViewCells 我已经学习了很多教程和网站,但没有解决我的问题 我曾尝试制作一个simpleCustomCells应用程序,但数据没有加载到表视图中 1.我有一个表视图控制器:Iphone 无法在UITableView的自定义单元格中加载数据,iphone,ios,objective-c,uitableview,Iphone,Ios,Objective C,Uitableview,请帮助我使用UITableViewCells 我已经学习了很多教程和网站,但没有解决我的问题 我曾尝试制作一个simpleCustomCells应用程序,但数据没有加载到表视图中 1.我有一个表视图控制器: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Ce
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [NSArray arrayWithArray:[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]];
for(id obj in nib)
{
if([obj isKindOfClass:[UITableViewCell class]])
{
cell = (CustomCell *)obj;
}
}
}
cell.label.text = @"hello";
return cell;
}
也试过
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
“在内,如果(单元格==nill)
”
2.我已经创建了CustomCell:UITableViewCell,其中只有一个UILabel插座。更改了CustomCell.xib标识符的名称
当我运行此应用程序时,会出现以下错误:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x71627f0> setValue:forUndefinedKey:]: this class
is not key value coding-compliant for the key label.
由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类
不符合密钥标签的密钥值编码。
请帮我解决这个问题,我哪里出了问题。为什么不给你的单元格一个标识符,并用dequewithreusableidentifier实例化它
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[AanbodTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.Label.text =@"Hello"
return cell;
}
我也会参考这本非常好的教程我想你应该这样添加,我希望它能起作用
static NSString *CellIdentifier = @"Cell";
customcellCell *cell = (customcellCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; <- set your customclass in this line.
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"customviewcell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.label.text = @"hello";
return cell;
静态NSString*CellIdentifier=@“Cell”;
customcellCell*单元格=(customcellCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier] 使用以下方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ContactListCell *contactCell;
NSString *contactCellID = [NSString stringWithFormat:@"ContactCell%d%d",indexPath.row,indexPath.section];
contactCell = (ContactListCell *)[tableView dequeueReusableCellWithIdentifier:contactCellID];
if(!contactCell) {
contactCell = [[ContactListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:contactCellID];
contactCellID = nil;
}
strName = @"First name";
contactCell.lblFirstName.text = [strName capitalizedString];
return contactCell;
}
用于自定义cell.h文件
@interface ContactListCell : UITableViewCell {
UILabel *lblFirstName;
}
@property (strong, nonatomic) UILabel *lblFirstName;
@end
@implementation ContactListCell
@synthesize lblFirstName;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
lblFirstName = [[UILabel alloc]initWithFrame:CGRectMake(kxiPadFirstNameCellLandscape, kyiPadFirstNameCellLandscape, kiPadFirstNameCellWidthLandscape, kiPadFirstNameCellHeightLandscape)];
lblFirstName.backgroundColor= [UIColor clearColor];
lblFirstName.numberOfLines = 0;
lblFirstName.textColor = kCustomCellFontColor;
[self.contentView addSubview:lblFirstName];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
用于自定义cell.m文件
@interface ContactListCell : UITableViewCell {
UILabel *lblFirstName;
}
@property (strong, nonatomic) UILabel *lblFirstName;
@end
@implementation ContactListCell
@synthesize lblFirstName;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
lblFirstName = [[UILabel alloc]initWithFrame:CGRectMake(kxiPadFirstNameCellLandscape, kyiPadFirstNameCellLandscape, kiPadFirstNameCellWidthLandscape, kiPadFirstNameCellHeightLandscape)];
lblFirstName.backgroundColor= [UIColor clearColor];
lblFirstName.numberOfLines = 0;
lblFirstName.textColor = kCustomCellFontColor;
[self.contentView addSubview:lblFirstName];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
好吧,我想我知道你被卡住了
步骤1:从xib文件中断开插座UILabel
步骤2:转到您的xib文件,您将在xib文件的左侧看到您的对象列表
步骤3:将鼠标指针放在写入对象的正下方。您将发现自定义单元格
已写入
步骤4:按ctrl键并将其拖动到标签上,然后将插座连接到标签上
第5步:清理程序并再次运行
祝你一切顺利。如果您还有疑问,我将详细说明。:) 你看到了吗?是的,我试过了,得到了同样的错误。为什么你要测试它是否是UITableViewCell
,而它需要是CustomCell
?你是否将单元格的类设置为自定义类?你有CustomCell.m/h文件吗?是的,我有CustomCell.h/m文件。我使用的是CustomCell而不是UtableViewCell。你们有解决方案吗?是的,我也使用了手机识别码。我仍然没有解决我的问题。是的,我认为标签的定义是正确的:@property(非原子,弱)IBOutlet UILabel*label;插座连接不正确。我通过右键点击标签建立了连接。现在我通过右键单击CustomCell来完成。令人惊讶的是,它奏效了。谢谢,我现在拿到了。我通过点击鼠标右键连接标签。我按你说的做了,点击CustomCell的右边。成功了。谢谢