2012-02-05 42 views
18

我想使用XIB創建自定義UITableViewCell,但我不確定如何使用UITableViewController的排隊機制來回收它。我怎樣才能做到這一點?如何回收從XIB創建的UITableViewCell對象?

民間,這個問題是打算自我回答根據the FAQ,雖然我喜歡真棒迴應。有一些upvotes,對待自己的啤酒。我問這是因爲一位朋友問我,我想把它放在StackOverflow上。如果你有任何貢獻,一切手段!

回答

51

如果你使用的是iOS 5中,您可以使用

[self.tableView registerNib:[UINib nibWithNibName:@"nibname" 
              bundle:nil] 
    forCellReuseIdentifier:@"cellIdentifier"]; 

然後,每當你撥打:

cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"]; 

的實現代碼如下要麼加載筆尖,給你一個細胞,或出隊細胞爲你!

該筆尖只需要一個在其內部定義了單個tableviewcell的筆尖!

+3

不知道這個。 – Moshe 2012-02-05 16:41:53

+2

這是一個iOS5引入的新API--自從我看到它之後一直使用它 - 讓生活變得如此簡單!通過繼承UITableViewCell並添加指向單元中各種控件的IBOutlet屬性,您甚至可以獲得更高級的功能,因此您甚至不需要再使用ViewWithTag! – 2012-02-05 16:46:42

+0

絕對真棒! – Moshe 2012-02-05 17:40:12

2

創建一個空的筆尖並添加表格單元格作爲第一項。在檢查器中,您可以在Interface Builder中添加reuseIdentifier字符串。

要在代碼中使用的電池,這樣做:

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *reuseIdentifier = @"blah"; //should match what you've set in Interface Builder 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[NSBundle mainBundle] loadNibNamed:@"YourTableCellNib" owner:nil options:nil] objectAtIndex:0]; 
    } 

    //set up cell 

    return cell; 
} 

還有就是你創建你的出口和使用控制器作爲文件的所有者裝入電池筆尖的另一種方法,但說實話這要容易得多。

如果您希望能夠訪問已添加到筆尖單元格中的子視圖,請爲其指定唯一標記並使用[單元格viewWithTag:x]訪問它們;

如果您希望能夠在單元格上設置自定義屬性,您需要創建一個自定義UITableViewCell子類,然後在InterfaceBuilder中將其設置爲您的筆尖的類,並在將UITableViewCell轉換爲您的自定義子類時你在上面的代碼中取出它。

1

這裏怎麼可以這樣做:

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    YourCustomeCell *cell = (YourCustomeCell *)[tableView dequeueReusableCellWithIdentifier:CellClassName]; 
    if (!cell) 
    { 
    NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil]; 
    cell = [topLevelItems objectAtIndex:0]; 
    } 

    return cell; 
} 

cellLoader在.H定義如下:

UINib *cellLoader; 

和.M是istantiated(初始化期間爲例)如下:

cellLoader = [[UINib nibWithNibName:CellClassName bundle:[NSBundle mainBundle]] retain]; 

and CellClassName在.m中定義如下(is als o你的xib的名字)。

static NSString *CellClassName = @"YourCustomeCell"; 

不要忘記在xib創建的單元格中使用字符串CellClassName

欲瞭解更多信息,我建議你閱讀這個夢幻般的教程creating-a-custom-uitableviewcell-in-ios-4

希望它有幫助。

P.S.我建議你使用UINib,因爲它是加載xib文件的優化方法。

2

要建立一個自定義的UITableViewCell使用廈門國際銀行,你要做的幾件事情:

  • 建立一個IBOutlet在頭
  • 配置在Interface Builder中的表視圖細胞
  • 裝入tableView:cellForRowAtIndexPath:
  • 配置它像任何其他細胞

所以內部XIB ......讓我們建立一個IBOutlet在頭文件中。

@property (nonatomic, retain) IBOutlet UITableViewCell *dvarTorahCell; 

不要忘記在實現文件中綜合它。

@synthesize dvarTorahCell; 

現在,我們來創建和配置單元。你要注意小區標識和IBOutlet中如下圖所示:

enter image description here

現在的代碼,你的廈門國際銀行加載到您的細胞,如下所示:

enter image description here

請注意,Interface Builder中的單元標識符與下面代碼中顯示的匹配。

然後你繼續前進,像其他任何配置你的細胞。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (cell == nil) { 

    [[NSBundle mainBundle] loadNibNamed:@"YUOnlineCell" owner:self options:nil]; 
    cell = dvarTorahCell; 
    dvarTorahCell = nil; 
} 

//configure your cell here. 

只要注意在訪問子視圖,如標籤時,你現在需要通過標籤的屬性名稱,如textLabeldetailTextLabel參考,而不是給他們。