我是 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?//
}
Step-1
In your 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
}
Step-2
让我们制作淡入淡出动画。UITableViewDelegate
has 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
,一切都会好起来的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)