开发者

Why can't I reuse any UITableView cells?

开发者 https://www.devze.com 2023-04-05 07:31 出处:网络
I have a UITableView showing over 50 custom-designed cells. 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

I have a UITableView showing over 50 custom-designed cells. 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.

What could be causing this problem? Here's a code snipped from my UITableViewSource.GetCell() method

var dequeuedCell=tableView.DequeueReusableCell (identifier);

    //try to reuse a previously recycled cell, cr开发者_如何学JAVAeate 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...


Have you set the cell identifier in your xib file? If this is not set, your cells won't be dequeued.


In the interface, declare: TableViewCell * aCell, and try using this method:

- (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!


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


Make sure that the "identifier" is an NSString, as this could be used as a token internally, instead of a a string-content comparison.

So:

NSString identifier = new NSString ("myID");

0

精彩评论

暂无评论...
验证码 换一张
取 消