2011-09-15 24 views
1

我有一個UITableView顯示超過50個自定義設計的單元格。表格顯示確定,但如果我滾動表格,則沒有單元格可以重複使用。相反,我的應用只是不斷加載新單元格,這對我的iPhone 3來說非常緩慢。爲什麼我不能重用任何UITableView單元?

什麼可能導致此問題?下面是從我UITableViewSource.GetCell()方法

var dequeuedCell=tableView.DequeueReusableCell (identifier); 

    //try to reuse a previously recycled cell, create a new one if none exists 
    var cell = (ITimeEntryTableCell)dequeuedCell; 
    if (cell == null) 
    { 
     //load a new cell using the XIB file definition 
     NSBundle.MainBundle.LoadNib (identifier, tableViewController, null); 
     cell = tableViewController.DurationCell; 
     tableViewController.DurationCell = null; 

    } 

由於剪斷代碼,

阿德里安

編輯:請注意,我使用MonoTouch的,沒有目標C ...

回答

3

您是否在xib文件中設置了單元標識符?如果沒有設置,您的單元格將不會被取出。

+0

如果有人像我一樣困惑:在設計師中,它被稱爲「恢復ID」而不是「標識符」。 – Zerga

+0

@Zerga不,它不是。恢復ID是關於狀態恢復。標識符是使用的正確字段。 – jrturton

0

這是完美的代碼 -

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellID = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 
    if(cell == nil) 
    { 
     [[NSBundle mainBundle] loadNibNamed:@"cell1" owner:self options:nil]; 
     cell = self.oneMessage; 
     self.oneMessage = nil; 
    } 
    return cell; 
} 

更新:對MonoTouch的,試試這個 -

public override UITableViewCell GetCell(UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) 
{ 
    UITableViewCell cell = tableView.DequeueReusableCell(this._cellIdentifier); 

    myTableViewCell mycell = null; 
    if (cell == null) 
    { 
     mycell = new myTableViewCell(); 
     NSBundle.MainBundle.LoadNib("RootViewController", _controller, null); 
     _controller.myTableCell = new myTableViewCell(); 
     mycell = _controller.myTableCell; 
     cell = new UITableViewCell(UITableViewCellStyle.Subtitle, this._cellIdentifier); 
    } 
    else 
    { 
     mycell = (myTableViewCell)cell; 
    } 
+0

我確定它有,但我使用Monotouch,所以我不能複製和粘貼它。如果您發現我在OP中粘貼的代碼存在任何問題,請隨時通知我。 –

+0

您發佈的Monotouch代碼使用與我的代碼相同的方法。但是,我的代碼的問題在哪裏?這就是我想知道的。 –

+0

你確定標識符設置正確嗎?因此我沒有看到你的代碼有任何問題 –

0

在接口方面,聲明:TableViewCell * aCell,並嘗試使用這種方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; { 
    static NSString * CellIdentifier = @"TableViewCell"; 
    TableViewCell * cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
    [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil]; // hooks up cell for us. 
    cell = aCell; 
    } 
    return cell; 
} 

希望幫助!

+0

不幸的是,我並沒有真正的幫助,因爲我使用Monotouch,所以我甚至無法閱讀,更不用說理解你發佈的代碼的細微差別了......感覺自由讓我知道你是否看到我的代碼有任何問題。 –

0

確保「標識符」是一個NSString,因爲這可以用作內部令牌,而不是字符串內容比較。

所以:

的NSString標識符=新的NSString( 「本身份識別碼」);

相關問題