2017-04-06 99 views
-1

我有UIView添加的类UITableView 现在我创建了UITableViewCell想在UITableViewCell上添加自定义单元格。如何在UIView TableView上添加UITableViewCell

- (id)initWithCoder:(NSCoder *)coder{ 

    self = [super initWithCoder:coder]; 
    if (self) { 
      //do somthing 
     filterColorTableView.delegate = self; 
     filterColorTableView.dataSource = self; 

     [self.filterColorTableView registerNib:[UINib nibWithNibName:@"FilterColor" bundle:nil] forCellReuseIdentifier:@"COLORCELL"]; 

     colorNameList = [[ColorModelClass colorListNames]allKeys]; 
     filterColorTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; 

    } 

    return self; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    static NSString *tableViewCellIdentifier = @"COLORCELL"; 
    FilterColorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier forIndexPath:indexPath]; 
    [cell.lbl_ColorName setText:@"Welcome"] ; 
    cell.lbl_ColorName.textColor = [UIColor redColor]; 
    [cell.colorImage setImage:[UIImage imageNamed:@"circle.png"]]; 
    return cell; 

} 

Crash Report Message: 

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier COLORCELL - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' 

即创建单元对象,但相关lbl_ColorName和colorImage是零送花儿给人的

注:我不使用的UIViewController只是UIView类是在这种方法中使用。

关于UIView Xib添加了UITableView,并在同一文件的所有者中使用了另一个自定义UITableViewCell。随时随地

enter image description here

+0

你注册用的tableview细胞?为了出队吗? – HarmVanRisk

+0

@HarmVanRisk是的,我做到了。 – kiran

+0

突出显示xib文件中的单元格时,是否已将标识符添加到“属性”检查器? – HarmVanRisk

回答

0

首先register the custom cell class在或viewDidLoad中之后。这样做后,the dequeue method与类或笔尖注册一起引入是dequeueReusableCellWithIdentifier:forIndexPath:。请注意新的indexPath参数。

它总是返回一个初始化的细胞,所以零检查是多余的...

FilterColorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:filterTableViewCell forIndexPath:indexPath]; 
// deleted if(cell == nil){...} 
+0

我尝试注册并更新其现在崩溃的'NSInternalInconsistencyException',原因:'无法使用标识符COLORCELL出列单元格 - 必须为该标识符注册一个nib或一个类,或者在添加的标识符中连接一个故事板中的原型单元格' “COLORCELL”,我更详细地更新了这个问题。 – kiran

+0

嗯。我没有意识到这是在initWithCoder的上下文中。这听起来像是在init中正在完成的工作还为时尚早。你可以移动桌面设置的东西到'didLayoutSubviews' – danh

相关问题