gpt4 book ai didi

ios - 在 Swift 中逐行淡入 UITableViewCell

转载 作者:IT王子 更新时间:2023-10-29 05:46:51 25 4
gpt4 key购买 nike

我是 swift 的新手,我正在尝试拥有一个 UITableView 并且单元格将被动画化以一个接一个地出现。我怎样才能做到这一点?另外,如果新出现的单元格行不在屏幕上(隐藏在表格下方)。当每个单元格出现时,如何向上移动表格?

    var tableData1: [String] = ["1", "2", "3", "4", "5", "6", "7"]

override func viewWillAppear(animated: Bool) {
tableView.scrollEnabled=false
tableView.alpha=0.0
NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(3), target: self, selector: "animateTable", userInfo: nil, repeats: false)
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData1.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell
cell.lblCarName.textAlignment = NSTextAlignment.Justified;
return cell
}

func animateTable() {

//what should be the code?//
}

最佳答案

第一步

在初始化单元格的 cellForRowAtIndexPath 方法中,将其隐藏;

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell     {
let cell: TblCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TblCell
cell.continueLabel.textAlignment = .justified
cell.contentView.alpha = 0
return cell
}

第二步

让我们制作淡入淡出动画。 UITableViewDelegate 具有 willDisplayCell 方法,它能够检测到当您滚动到顶部或底部时,第一个单元格将显示在窗口上。

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
UIView.animate(withDuration: 0.8, animations: {
cell.contentView.alpha = 1
})
}

您的淡入淡出动画正在进行中。最重要的是,您不能在运行时直接设置单元格的 alpha,因为 iOS 正在对单元格进行一些特殊的内部处理,作为您的 UITableView 的一部分,并忽略您的设置。因此,如果您设置单元格的 contentView,一切都会好起来的。

关于ios - 在 Swift 中逐行淡入 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33274787/

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