ios - -[UITableView _configureCellForDisplay :forIndexPath:], 中的断言失败

标签 ios objective-c iphone xcode uitableview

我是 xcode 的新手,我正在尝试使用自定义单元格在 tableview 中显示一些数据,但我得到了 *** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:],错误。请任何人都可以指导我。提前致谢。我正在使用 Storyboard 来创建 UITableview。

这是我的Viewcontroller.m代码

#import "ViewController.h"
#import "CustomCell.h"
@interface ViewController ()

{
    NSArray *profilehd;
    NSArray *profileimg;
}

@end

@implementation ViewController

@synthesis tableview;

-(void)viewDidLoad
{
    tablevw.dataSource = self;
    tablevw.delegate = self;

    profilehd = [[NSArray alloc]initWithObjects:@"Name",@"Email",@"Skype", nil];
    profileimg = [[NSArray alloc]initWithObjects:@"about_me_icon.png",@"email_icon.png",@"skype_icon.png", nil];

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
}


#pragma mark:-UITableView Datasource and Delegate Methods

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [profilehd count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"CellID";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        cell.ProfilehdLabel.text = [profilehd objectAtIndex:indexPath.row];
        cell.profileImgvw.image = [UIImage imageNamed:[profileimg objectAtIndex:indexPath.row]];
      }

    return cell;
}

CustomCell.m

@property (strong, nonatomic) IBOutlet UILabel *profilehdLabel;
@property (strong, nonatomic) IBOutlet UIImageView *profileImgvw;

最佳答案

您永远不会创建一个单元格,您只是尝试重用一个出列的单元格。所以,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *cellIdentifier = @"CellID";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
       // if cell is empty create the cell
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

   cell.ProfilehdLabel.text = [profilehd objectAtIndex:indexPath.row];

cell.profileImgvw.image = [UIImage imageNamed:[profileimg objectAtIndex:indexPath.row]];

    return cell;
}

关于ios - -[UITableView _configureCellForDisplay :forIndexPath:], 中的断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33689818/

相关文章:

objective-c - 从头开始编写 Objective-C 类

ios - 我不应该在核心数据中存储什么?

ios - 如何在 SwiftUI 中位于 Apple Files App 的文件夹中写入文件?

ios - 标题上方有/没有图像的可调整大小的 UIButton?

ios - 如何用 RxSwift 的 DelegateProxy 替换协议(protocol)?

ios - 无法使具有标识符的单元出队

ios - 如何运行 Objective-C 类的子类的 Swift 方法,该方法具有同名方法

iphone - 基于 CSS 的框架,如 SCSS(自适应设计)

iphone - 使用 iphone 摄像头检测和识别数字

ios - 对于我的任务,您应该如何通过 3G 网络在 iPhone 设备之间进行通信?