繁体   English   中英

无法在UITableView的自定义单元中加载数据

[英]Cannot load data in custom cells in a UITableView

请帮我UITableViewCells。

我遵循了许多教程和网站,但是并没有解决我的问题。

我试图制作一个simpleCustomCells应用程序,但数据未加载到表视图中。

1.我有一个表视图控制器:

- (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];

“在if(cell==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.

请帮我解决这个问题,我哪里出问题了。

好吧,我想我到了你被卡住的地方。

步骤1:从xib文件中断开出口UILabel

第2步:转到xib文件,您将在xib文件的左侧看到对象列表。

步骤3:将鼠标指针放在写入对象的位置下方。 您会发现编写的Custom Cell

步骤4:按ctrl并将其拖动到标签,然后 “插座” 连接到标签。

步骤5: 清理程序,然后再次运行

祝一切顺利。 如果您仍有疑问,我会详细说明。 :)

我认为您应该这样添加,希望它能起作用

  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;

使用以下方式:

 - (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;
    }

对于自定义单元.h文件

@interface ContactListCell : UITableViewCell {

    UILabel *lblFirstName;
}

@property (strong, nonatomic) UILabel *lblFirstName;
@end

对于自定义单元.m文件

@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

为什么不给您单元格一个标识符,并使用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;
}

我也会参考这个很好的教程http://www.appcoda.com/customize-table-view-cells-for-uitableview/

暂无
暂无

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

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