【问题标题】:Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:],-[UITableView _configureCellForDisplay:forIndexPath:] 中的断言失败,
【发布时间】:2015-11-13 09:46:48
【问题描述】:

我是 xcode 的新手,我正在尝试使用自定义单元格在 tableview 中显示一些数据,我收到 *** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:],错误。请任何人指导我。提前致谢。我正在使用故事板来创建 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;

【问题讨论】:

  • 我认为您应该编辑问题代码部分的格式。

标签: ios objective-c iphone xcode uitableview


【解决方案1】:

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

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

【讨论】:

  • 无法在 tableview 单元格中加载 profilehd 和 profileimg 数据
  • 没有收到任何错误,单元格为空。我想加载 profilehd = [[NSArray alloc]initWithObjects:@"Name",@"Email",@"Skype", nil]; profileimg = [[NSArray alloc]initWithObjects:@"about_me_icon.png",@"email_icon.png",@"skype_icon.png", nil];
猜你喜欢
  • 2015-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-26
  • 2013-02-25
  • 2012-04-25
  • 2014-11-19
相关资源
最近更新 更多