2013-03-12 38 views
7
static NSString *cellIdentifier = @"cell"; 
if (tableView ==tableview1) 
{ 
    ContactCustom *cell1=(ContactCustom *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];   
    if (cell1 == nil) 
    { 
     [[NSBundle mainBundle] loadNibNamed:@"ContactCustom" owner:self options:nil]; 
     cell1 = contactCustom; 
    } 
} 

如何在撥打cellForRowAtIndex方法之前在viewDidLoad方法中註冊筆尖名稱?爲tableView註冊筆尖名稱

回答

0
static NSString *cellIdentifier = @"cell"; 
if (tableView ==tableview1) 
{ 
    ContactCustom *cell1=(ContactCustom *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];   
    if (cell1 == nil) 
    { 
     cell1 = [tableview dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 

    } 
} 
+0

越來越無法識別的選擇發送到實例錯誤 – user2134883 2013-03-12 12:26:50

+0

你檢查xib與類正確連接,標識符爲p roper set in xib and use below code cell = [tableview dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; – Wish 2013-03-12 12:36:28

+0

它應該是ContactCustom類中的Identifier屬性中的單元格 – user2134883 2013-03-12 12:43:39

1

蘋果的UITableView提供寄存器筆尖方法IOS 5 後請檢查​​類別參考 http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html

例:

 In view did load you can register nib for UITableView like below 
    [tableView registerNib:[UINib nibWithNibName:@"nibName" bundle:nil] forCellReuseIdentifier:@"identifienName"]; 

     In cellForRowAtIndexPath 
     cell = [tableView dequeueReusableCellWithIdentifier:@"identifienName"]; 
+0

出現error.How如何更改此代碼的tableView與customCell – user2134883 2013-03-12 12:39:48

+0

您收到哪個錯誤? – Narayana 2013-03-12 12:44:07

+0

***由於未捕獲的異常'NSUnknownKeyException',原因:'[ setValue:forUndefinedKey:]終止應用程序:此類不是關鍵值與代碼相關的關鍵contactCustom。 – user2134883 2013-03-12 12:48:42

0
// First of all Declare an IBOutlet for your customCell in Your View Controller 
IBOutlet ScoreCell *scoreCell; 
// Synthesize it. 
// Assign your view controller class to File Owner of your custom cell (Eg. File Owner of ScoreCell.xib) 
// Then Assign Reference Outlet of ScoreCell.xib with Object 'scoreCell' 
// Finally Create your custom Cell as follow : 

ScoreCell *cell = (ScoreCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
if (cell == nil) { 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 
0

寄存器包含與表的單元格的筆尖對象在指定的標識符下查看。

- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier 
Parameters 

筆尖 甲筆尖對象,指定筆尖文件以用於創建細胞。該參數不能爲零。 標識符 該單元的重用標識符。該參數不能爲零,也不能是空字符串。

doc可以幫助你很多

+0

你能舉個例子嗎? – user2134883 2013-03-12 13:09:39

17
(void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UINib *cellNib = [UINib nibWithNibName:@"cell" bundle:nil]; 
    [self.tableView registerNib:cellNib forCellReuseIdentifier:@"cell"]; 
} 
+0

這是正確的答案,經過數小時的相同問題的努力和嘗試其他所有提到這最終解決了我的問題。這樣做後,您可以從cellForRowAtIndex方法中刪除「if」語句 – Chris 2014-01-31 08:56:18

0

在這裏看到:http://mrmaksimize.com/Custom-UITableViewCell-With-NIB/

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     [self.tableView registerNib:[UINib nibWithNibName:@"EXCustomCell" 
          bundle:[NSBundle mainBundle]] 
      forCellReuseIdentifier:@"CustomCellReuseID"]; 
    } 

在後面的代碼:

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

    // Configure the cell... 
    [cell.cellItemImage setImage:[UIImage imageNamed:@"glyphicons_428_podium"]]; 
    [cell.cellItemLabel setText = @"Mr Burns."]; 
    return cell; 
}