【发布时间】:2014-04-17 09:28:50
【问题描述】:
我的应用有一个 UITableView,其中包含具有透明背景的可变高度单元格。针对 iOS 7 SDK 构建时,行删除动画存在问题:
注意某些单元格的闪烁,例如第 15、17、21 和 23 行。在 iOS
如果我做任何这些事情,问题就会消失(我还没有准备好做这些事情):
- 始终创建新单元,而不是使可重复使用的单元出列
- 为细胞提供非透明背景
- 赋予所有行相同的高度
我尝试过的其他方法都没有帮助。 iOS 7 上是否需要一些秘诀来获得平滑的行删除动画?
这是我可以将问题提炼为 [C#/Xamarin] 的最小测试用例:
public class MainViewController : UITableViewController {
public MainViewController() {
this.TableView.Source = new TableSource(this.TableView, 50);
}
public override void ViewDidAppear(bool animated) {
base.ViewDidAppear(animated);
NSTimer.CreateRepeatingScheduledTimer(0.5f, ((TableSource) this.TableView.Source).DeleteFirstRow);
}
}
public class TableSource : UITableViewSource {
private UITableView tableView;
private List<RowInfo> activeRows;
public TableSource(UITableView tableView, int numRows) {
this.tableView = tableView;
this.activeRows = new List<RowInfo>();
for (int i=1; i<=numRows; i++) {
this.activeRows.Add(new RowInfo(i));
}
}
public void DeleteFirstRow() {
if (this.activeRows.Count == 0) return;
this.activeRows.RemoveAt(0);
this.tableView.BeginUpdates();
this.tableView.DeleteRows(new NSIndexPath[] { NSIndexPath.FromRowSection(0, 0) }, UITableViewRowAnimation.Right);
this.tableView.EndUpdates();
}
public override int RowsInSection(UITableView tableview, int section) {
return this.activeRows.Count;
}
public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath) {
return this.activeRows[indexPath.Row].Height;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) {
const string CellID = "Cell";
UITableViewCell cell = tableView.DequeueReusableCell(CellID);
if (cell == null) {
cell = new UITableViewCell(UITableViewCellStyle.Default, CellID);
}
cell.TextLabel.Text = this.activeRows[indexPath.Row].Text;
return cell;
}
public override void WillDisplay(UITableView tableView, UITableViewCell cell, NSIndexPath indexPath) {
cell.BackgroundColor = UIColor.Clear;
}
private class RowInfo {
public RowInfo(int index) {
Random random = new Random(index);
this.Height = random.Next(20, 80);
this.Text = "Row " + index + " has height " + this.Height;
}
public float Height;
public string Text;
}
}
【问题讨论】:
-
它是在设备上以及模拟器上还是在模拟器上完成?
-
是的。在 iPhone 和 iPad sims、iPhone 5s 和 iPad 3 上看到的行为,都在 iOS 7.1 上。
-
你能解决这个问题吗?我也面临同样的问题。
-
我也遇到过很多这个问题,我发现的唯一解决方法(几乎不是解决方案)是切换到 UITableViewRowAnimationFade - 这似乎比右/左滑动面板运行得更顺畅' 类型动画。对不起:(
-
不幸的是,这在 iOS 8.3 中仍未修复。但是 user3099609 提出的解决方法是有效的。
标签: ios ios7 uitableview