Ios 有没有一种方法可以忽略为TableViewCell注册nib或类?

Ios 有没有一种方法可以忽略为TableViewCell注册nib或类?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我的表中只有一行带有UIImageView。当我像下面提到的那样直接分配图像时 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *identifier = @"cellidentifier"; UITableViewCell *cell = [tableView dequeueReusableCell

我的表中只有一行带有
UIImageView
。当我像下面提到的那样直接分配图像时

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

    NSString *identifier = @"cellidentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];

    // Configure the cell...
    cell.imageView.image = [self.backgroundImages objectAtIndex:indexPath.row];

    return cell;
}
它返回错误:

无法将标识符为cellidentifier的单元格出列-必须 为标识符注册nib或类,或连接原型 “故事板中的单元格”


因此,我想知道是否有一种方法可以忽略为
UITableViewCell
注册nib或类,以便在这种情况下缩短代码?

您只需在viewDidLoad中添加一行代码即可解决此错误

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellidentifier"];
}

如果要为单元格使用自定义类,则应注册该类(在viewDidLoad中),如果单元格是在xib文件中创建的,则使用
registerInB:forCellWithReuseIdentifier:

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
否则,按如下方式写入
cellforrowatinexpath
tableView委托:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    //Configure cell
   return cell;
}
在序列图像板tableviewcell标识符中指定相同的名称

具有此名称的cellidentifier。我只是把我的手机号码给我的手机


将您的单元标识符放在故事板中,如下面的屏幕截图所示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Songcell"; // copy this identifier and paste it into your storyboard tableviewcell indetifier 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    UIImageView *IMAGEVIEWNAME = (UIImageView*)[cell viewWithTag:YOUR_TAG]; // give tag to your imageview from storyboard and access it here 

    IMAGEVIEWNAME.image = [UIImage imageNamed:@"YOURIMAGENAME"];
    return cell;
}