我目前正在使用Monotouch(Xamarin)框架开发iOS应用。
我有一个自定义的tableview源,它使用自定义单元格,其单元格高度是动态计算的。
当我在iOS模拟器中运行项目时,如果我快速滚动到底部或顶部,顶部单元格会替换底部,反之亦然 - 好像它绘制不正确,或错误地重用错误的单元格? / p>
只是为了澄清 - 如果我的细胞是
一个 二 三 四 5
如果我从上到下快速滚动,我的单元格显示为
一个 二 三 四 一个
或者,如果我慢慢滚动到底部,细胞保持整齐,一旦我快速滚动到顶部,我就会
5 二 三 四 5
如果我偶尔上下滚动,细胞会随机混淆。
我的表格来源如下:
PostModel[] models;
string cellIdentifier = "FeedCell";
public FeedSource (PostModel[] items)
{
models = items;
}
public override int RowsInSection (UITableView tableview, int section)
{
return models.Length;
}
public override float GetHeightForRow (UITableView tableView, NSIndexPath indexPath)
{
FeedCell cell = this.GetCell (tableView, indexPath) as FeedCell;
var height = cell.height;
return height;
}
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
FeedCell cell = tableView.DequeueReusableCell (cellIdentifier) as FeedCell;
//if there are no cells to reuse, create a new one
if (cell == null) {
cell = new FeedCell (models [indexPath.Row], new NSString (cellIdentifier));
cell.LayoutSubviews ();
}
//cell.height = cell.height;
return cell;
}
我已经听说过动态细胞高度的性能问题,但我只测试了5个细胞,这看起来很奇怪。
它可能只是我的iOS模拟器,这不会在设备上发生吗?有没有解决这个问题?
答案 0 :(得分:4)
尽可能重复使用单元格。逻辑是给我任何可用的单元格(DequeueReusableCell
),如果没有可用的单元格(cell == null
),则创建一个新的单元格(new FeedCell
)。
因此,您不应在创建单元格时固定单元格的内容。您只需要创建一个新的空单元格。
拥有一个单元格后,就可以填充该索引路径所需内容的单元格。
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
FeedCell cell = tableView.DequeueReusableCell (cellIdentifier) as FeedCell;
//if there are no cells to reuse, create a new one
if (cell == null) {
cell = new FeedCell (new NSString (cellIdentifier));
}
cell.model = models[indexPath.row]; // assuming you can do something like this.
cell.layoutSubviews();
return cell;
}