MonoTouch表格单元格未重复使用(DequeueReusableCell)

时间:2013-02-17 09:37:09

标签: xamarin.ios

Update1:​​感谢Greg Munn& Json看看我的代码,在目前阶段,我认为这可能是由于我使用的是MonotTouch的评估版本。

Update2:我粘贴了版本信息,输出也已更新。

Update3:在我安装了monotouch 2.0之后,我能够将应用程序部署到设备,它确实发生在真实设备上。但它在模拟器上继续出现相同的症状。

我得到了从UITableViewSource派生的自定义类(TableSource),我发现只要我尝试在模拟器(6.1)中滚动表视图,它就会创建整个1000个单元格。我还在尝试monoTouch,我不确定它在真实设备上运行时的行为是什么。当我尝试滚动tableview时,我真的不知道它为什么会创建1000个单元格。我错过了什么吗?

以下是输出的一部分:

Console.Out.WriteLine(string.Format("Created: {0} Reused: {1} - currentRow: {2}", this._totalRowCreated, this._totalRowReused, indexPath.Row.ToString()));
  • 2013-02-20 17:43:16.638 TableViewTest [3039:c07]新建row999
  • 2013-02-20 17:43:16.638 TableViewTest [3039:c07]创建时间:1000重用:0 - currentRow:999
  • 2013-02-20 17:43:16.638 TableViewTest [3039:c07]创建新的row1000
  • 2013-02-20 17:43:16.639 TableViewTest [3039:c07]创建时间:1001重用:0 - currentRow:1000
  • 2013-02-20 17:43:21.581 TableViewTest [3039:c07]创建新行11
  • 2013-02-20 17:43:21.582 TableViewTest [3039:c07]创建时间:1002重用:0 - currentRow:11
  • 2013-02-20 17:43:30.650 TableViewTest [3039:c07]重用row1001
  • 2013-02-20 17:43:30.650 TableViewTest [3039:c07]创建时间:1002重用:1 - currentRow:1001
  • 2013-02-20 17:43:30.651 TableViewTest [3039:c07]创建新的row1002
  • 2013-02-20 17:43:30.651 TableViewTest [3039:c07]创建时间:1003重用:1 - currentRow:1002
  • 2013-02-20 17:43:30.904 TableViewTest [3039:c07]创建新row12
  • 2013-02-20 17:43:30.904 TableViewTest [3039:c07]创建时间:1004重用:1 - currentRow:12
  • 2013-02-20 17:43:30.905 TableViewTest [3039:c07]创建新的row0
  • 2013-02-20 17:43:30.906 TableViewTest [3039:c07]创建时间:1005重用:1 - currentRow:0

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        this.window = new UIWindow(UIScreen.MainScreen.Bounds);
        this.window.MakeKeyAndVisible();
    
        this.viewController = new TableViewTestViewController();
    
    
        this.viewController.View.Frame = new RectangleF(
                0, 
                UIApplication.SharedApplication.StatusBarFrame.Height, 
                UIScreen.MainScreen.ApplicationFrame.Width, 
                UIScreen.MainScreen.ApplicationFrame.Height);
    
        this.window.RootViewController = viewController;
    
        return true;
    }
    

private TableSource _tblSrc;

public override void ViewDidLoad()
{
  base.ViewDidLoad();

  // Perform any additional setup after loading the view, typically from a nib.

  if (this._tblSrc == null)
  {
    this._tblSrc = new TableSource();

    for (int i = 0; i < 1000; i++)
    {
      this._tblSrc.DataItems.Add(string.Format("Row {0}", i));
    }

    this.TableView.Source = this._tblSrc;
  }
}

//---------------------------------

namespace TableViewTest.Models
{
    public class TableSource : UITableViewSource
    {
        private string _cellIndentifier = "MyCellId";
        private List<string> _dataItems;
        private int _totalRowCreated = 0;
        private int _totalRowReused = 0;

        public TableSource()
        {
        }

        public List<string> DataItems
        {
            get{ return this._dataItems ?? (this._dataItems = new List<string>());}
        }

        public override int RowsInSection(UITableView tableview, int section)
        {
            return this.DataItems.Count;
        }

        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            var cell = tableView.DequeueReusableCell(this._cellIndentifier);

            if (cell == null)
            {  
                this._totalRowCreated ++;
                Console.Out.WriteLine("Create new row" + indexPath.Row.ToString());
                cell = new UITableViewCell(UITableViewCellStyle.Default, this._cellIndentifier);
                cell.Tag = indexPath.Row;
            } else
            {
                this._totalRowReused ++;
                Console.Out.WriteLine("Reuse row" + indexPath.Row.ToString());
            }

            Console.Out.WriteLine(string.Format("Created: {0} Reused: {1}", this._totalRowCreated, this._totalRowReused));


            var value = this.DataItems [indexPath.Row];
            cell.TextLabel.Text = value;

            return cell;
        }
    }
}

MonoDevelop 3.1.1
Installation UUID: 4098f059-8936-47d8-8e66-4e501f33a58b
Runtime:
  Mono 2.10.9 (tarball)
  GTK 2.24.10
  GTK# (2.12.0.0)
  Package version: 210090011
Apple Developer Tools:
   Xcode 4.6 (2066)
   Build 4H127
Xamarin.Mac: Not Installed
Monotouch: 6.0.10 (Evaluation)
Mono for Android: Not Installed

Build information:
  Release ID: 30101000
  Git revision: 5d928ec4f9d5864b4db04a1301b8a8649b43fb9d
  Build date: 2012-12-14 19:11:30+0000
  Xamarin addins: 80f2dcc8fe4ed316b3e77dde496fc33d90305047
Operating System:
  Mac OS X 10.8.2

0 个答案:

没有答案