gpt4 book ai didi

ios - UITableView 行删除动画在 iOS 7 上闪烁

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:05:34 28 4
gpt4 key购买 nike

我的应用有一个 UITableView,其中包含具有透明背景的可变高度单元格。针对 iOS 7 SDK 进行构建时,行删除动画存在问题:

UITableView cell delete animation

请注意某些单元格的闪烁,例如第 15、17、21 和 23 行。在 iOS <= 6 上未观察到此行为。

如果我做了这些事情中的任何一个,问题就会消失(我还没有准备好做任何事情):

  • 总是创建新的单元而不是将可重复使用的单元出列
  • 为单元格提供非透明背景
  • 给所有行相同的高度

我尝试过的其他方法都没有帮助。在 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;
}
}

最佳答案

TLDR:cell.clipsToBounds = YES;

====

这是我发现的:

'   |    |    |    | <UITableViewCell: 0x79e81910; frame = (0 232; 320 32); text = 'row 27'; clipsToBounds = YES; autoresize = W; animations = { position=<CABasicAnimation: 0x79e8f020>; }; layer = <CALayer: 0x79e81aa0>>
| | | | | <UITableViewCellScrollView: 0x79e81ad0; frame = (0 0; 320 32); autoresize = W+H; gestureRecognizers = <NSArray: 0x79e81d60>; layer = <CALayer: 0x79e81ca0>; contentOffset: {0, 0}>
| | | | | | <UIView: 0x79e8b7d0; frame = (0 0; 320 51); clipsToBounds = YES; layer = <CALayer: 0x79e8b830>>
| | | | | | <UITableViewCellContentView: 0x79e81f50; frame = (0 0; 320 31.5); gestureRecognizers = <NSArray: 0x79e82160>; layer = <CALayer: 0x79e81fc0>>
| | | | | | | <UILabel: 0x79e821b0; frame = (15 0; 290 31.5); text = 'row 27'; userInteractionEnabled = NO; layer = <CALayer: 0x79e82260>>
| | | | | | <_UITableViewCellSeparatorView: 0x79e824d0; frame = (15 31.5; 305 0.5); layer = <CALayer: 0x79e82540>>

如果仔细查看第 27 行,您会注意到单元格的高度为 32 像素。单元格的 subview 是一个 ScrollView (我在某处读到它是您向左滑动以显示 iOS7 中的行操作的 ScrollView )。正如我们所见,contentView 的正确高度为 32px,但是,有趣的部分从这里开始:在 contentView 后面有一个 View ,其高度为 51px。我相信这是罪魁祸首。

不幸的是,我找不到那个 View (它不是 backgroundView,不是 selectedBackgroundView,等等,简而言之:它不是 UITableViewCell 类的公共(public)属性中的任何 View )。

另外,奇怪的是,该 View 的背景颜色是 clearColor。理论上它甚至不应该是可见的!我相信,我们在这里处理的是一些 CoreAnimation 优化,它不会渲染这个神秘 View 后面的区域。

因此,发生的情况是:1) 创建单元格 2) 调整单元格大小并准备好呈现 3) 单元格添加其神秘 View 并添加正确大小 4) 单元格被重新使用 5) 单元格调整为新大小动态高度 6) scrollView 和 contentView 也会调整大小,但神秘 View 不会(请注意它没有设置任何调整大小掩码)。

这解释了为什么您能够通过上述三种方法中的任何一种解决此问题。

解决方法是在创建时设置单元格的 cell.clipsToBounds = YES;。我还建议不要设置 cell.backgroundColor。 IIRC,您应该为此使用 cell.backgroundView(将其设置为新创建的 View 并将其设置为 backgroundColor)。然而,单元格的默认 bacgroundColor 无论如何都是清晰的,因此在这种情况下这并不重要。

我很高兴听到有人告诉我这个私有(private) View 是干什么用的。

关于ios - UITableView 行删除动画在 iOS 7 上闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23129379/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com