简体   繁体   English

为什么我不能重用任何UITableView单元格?

[英]Why can't I reuse any UITableView cells?

I have a UITableView showing over 50 custom-designed cells. 我有一个UITableView显示了50多个自定义设计的单元格。 The table shows ok, but if I scroll the table, no cells ever get reused. 该表显示正常,但是如果我滚动该表,则不会再使用任何单元格。 Instead my app just keeps loading new cells all the time, which is very slow on my iPhone 3. 取而代之的是,我的应用程序一直一直在加载新的单元格,这在我的iPhone 3上非常慢。

What could be causing this problem? 什么可能导致这个问题? Here's a code snipped from my UITableViewSource.GetCell() method 这是从我的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;

    }

Thanks, 谢谢,

Adrian 阿德里安

Edit: Please note that I am using Monotouch, not objective C... 编辑:请注意,我使用的是Monotouch,而不是客观的C ...

Have you set the cell identifier in your xib file? 您是否在xib文件中设置了单元标识符? If this is not set, your cells won't be dequeued. 如果未设置,您的单元将不会出队。

This is the code that works perfectly - 这是完美的代码-

- (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;
}

UPDATE: for monotouch, try this - 更新:对于单点触控,请尝试以下操作-

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;
    }

In the interface, declare: TableViewCell * aCell , and try using this method: 在接口中,声明: 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;
}

Hope that Helps! 希望有帮助!

Make sure that the "identifier" is an NSString, as this could be used as a token internally, instead of aa string-content comparison. 确保“标识符”是NSString,因为它可以在内部用作令牌,而不是字符串内容比较。

So: 所以:

NSString identifier = new NSString ("myID"); NSString标识符=新的NSString(“ myID”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM