2012-08-09 63 views
0

我正在使用自定義單元格(This site)。這是在我的舊應用程序的工作,但在這個應用程序,我不知道爲什麼它沒有工作,當我運行上面的代碼,他卡在綠色錯誤行(NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];)。Iphone:使用表格自定義單元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    CellIdentifier = @"CustomCell"; 
    PCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil){ 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
     for (id currentObject in topLevelObjects){ 
      if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
       cell = (PCustomCell *) currentObject; 
       break; 
      } 
     } 
    } 
    P2Dict = [P2Rows objectAtIndex: indexPath.row]; 
    cell.Contracts.text = [P2Dict objectForKey:@"Contracts"]; 
    return cell; 
} 

http://ar2.co/SS2.png

+0

你有你的新項目名爲CustomCell一個的.xib文件? – Eyal 2012-08-09 17:31:00

+0

http://ar2.co/SS2.png – user1526898 2012-08-09 17:32:02

+0

PCustomCell.xib – user1526898 2012-08-09 17:32:21

回答

0

有沒有遇到的UITableViewCell(PCustomCell)在CustomCell.xib? 你應該在這裏顯示錯誤的截圖。這將會有所幫助。

+0

http://ar2.co/SS2.png – user1526898 2012-08-09 17:32:38

0

參考以下過程。如何創建自定義表格單元格是一種。

VocaTableCell就是我的情況。做一個你的CustomTableCell。

enter image description here enter image description here


#import <UIKit/UIKit.h> 

@interface VocaTableCell : UITableViewCell 
{ 
    UILabel *vocaLabel; 
} 
@property (nonatomic, retain) IBOutlet UILabel *vocaLabel; 

@end 

#import "VocaTableCell.h" 


@implementation VocaTableCell 
@synthesize vocaLabel; 

- (void)dealloc { 
    [vocaLabel release]; 
    [super dealloc]; 
} 
@end 

下面的代碼是指Befoe,你是正確做出customTableCell?在列表下面檢查plz。

有幾種創建customTablecell的方法。

我的態度很受歡迎。

  1. 文件所有者的CustomTabelCell應該是一個NSObject的。 (非常重要默認是也許UITableCell。你必須是一個改變文件的所有者NSObject的(NSObject的意思是ID類型!))

  2. CustomTableCell鏈接對象類的CustomTableCell類。

  3. 引用出口鏈接不是文件的所有者,必須是您的直接鏈接CustomTableCell。


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Custom"; 

    CustomTableCell *cell = (CustomTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (!cell) 
    { 
     UINib *nib = [UINib nibWithNibName:@"CustomTableCell" bundle:nil]; 
     NSArray *arr = [nib instantiateWithOwner:nil options:nil]; 
     cell = [arr objectAtIndex:0]; 
    } 

    cell.textLabel.text =[arr objectAtIndex:indexPath.row]; 
} 
+0

嗨你的第二張圖片我沒有我的.h對象在customCell參考出口 – user1526898 2012-08-09 17:37:33

+0

你做了一個customtablecell類嗎?你必須上課。 File-New-File-Objective-c Class - CustomTableCell SubclassOf UITableViewCell – 2012-08-09 17:40:51

0

你的錯誤狀態:CustomCell 「 」「 無法在指定的包加載NIB」。所以存在你的問題....你的xib命名爲什麼?

1

自定義單元格xib文件帶一個tablecell.and在custom.m文件中實現。在視圖控制器中設置整個tableview通過viewcontroller xib或以編程方式.as選擇。並設置委託和datasourece methods.in數據源方法編寫的定製cell.code下面寫

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Custom"; 

CustomTableCell *cell = (CustomTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (!cell) 
{ 
    UINib *nib = [UINib nibWithNibName:@"CustomTableCell" bundle:nil]; 
    NSArray *arr = [nib instantiateWithOwner:nil options:nil]; 
    cell = [arr objectAtIndex:0]; 
} 

cell.textLabel.text =[arr objectAtIndex:indexPath.row]; 

}