2017-04-06 105 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

相關問題