我有一个UITableView,在tableViewCell里面我有一个UICollectionView。
我的要求是在点击第一个tableView单元格的按钮时,我必须为第二个tableViewCell设置动画。
以下是我的代码: -
//Cell For Row at indexPath
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (0 == indexPath.section) {
let cell = tableView.dequeueReusableCell(withIdentifier: "FirstRowCell") as! FirstRowCell
cell.btnReview.addTarget(self, action: #selector(GotoReview), for: UIControlEvents.touchUpInside)
cell.btnMyProduct.addTarget(self, action: #selector(AnimateCollectionView), for: UIControlEvents.touchUpInside)
//cell.
return cell
} else if ( 1 == indexPath.section) {
let identifier = "TableCollectionCell"
var tableCollectionCell = tableView.dequeueReusableCell(withIdentifier: identifier) as? TableCollectionCell
if(tableCollectionCell == nil) {
let nib:Array = Bundle.main.loadNibNamed("TableCollectionCell", owner: self, options: nil)!
tableCollectionCell = nib[0] as? TableCollectionCell
tableCollectionCell?.delegate = self
}
return tableCollectionCell!
} else {
let identifier = "BrandImagesTableCell"
var brandImagesTableCell = tableView.dequeueReusableCell(withIdentifier: identifier)
if(brandImagesTableCell == nil) {
let nib:Array = Bundle.main.loadNibNamed("BrandImagesTableCell", owner: self, options: nil)!
brandImagesTableCell = nib[0] as? BrandImagesTableCell
}
//brandImagesTableCell.
return brandImagesTableCell!
}
}
在我的代码中,您可以看到:
if (0 == indexPath.section)
因为我有一个按钮目标(#selector(AnimateCollectionView)
)
我想动画tableCollectionCell,它位于(1 == indexPath.section
)。
请参阅我的AnimateCollectionView
方法: -
func AnimateCollectionView() {
let identifier = "TableCollectionCell"
var tableCollectionCell = tableView.dequeueReusableCell(withIdentifier: identifier) as? TableCollectionCell
if(tableCollectionCell == nil) {
let nib:Array = Bundle.main.loadNibNamed("TableCollectionCell", owner: self, options: nil)!
tableCollectionCell = nib[0] as? TableCollectionCell
tableCollectionCell?.delegate = self
}
tableCollectionCell?.alpha = 0
UIView.animate(withDuration: 1.50, animations: {
//self.view.layoutIfNeeded()
tableCollectionCell?.alpha = 1
})
}
答案 0 :(得分:0)
如果要为其更改设置动画,可以只更改一些状态变量,调用tableView.reloadRows(at:with:)
,然后让cellForRowAt
检查这些状态变量,以了解最终单元格应该是什么样的(即是否是"之前"或"之后"单元格配置)。
例如,这是一个示例,有两个单元格,将第二个单元格从红色单元切换到蓝色单元格。
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "RedCell", bundle: nil), forCellReuseIdentifier: "RedCell")
tableView.register(UINib(nibName: "BlueCell", bundle: nil), forCellReuseIdentifier: "BlueCell")
}
@IBAction func didTapButton(_ sender: UIButton) {
isRedCell = !isRedCell
tableView.reloadRows(at: [IndexPath(row: 1, section: 0)], with: .fade)
}
var isRedCell = true
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "ButtonCell", for: indexPath)
return cell
} else if indexPath.row == 1 {
if isRedCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RedCell", for: indexPath)
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "BlueCell", for: indexPath)
return cell
}
}
fatalError("There are only two rows in this demo")
}
}
现在,假设您确实需要使用NIB而不是单元原型,我会使用上面的tableview注册NIB,而不是让cellForRow
必须自己手动实例化它们。同样,对于带按钮的单元格,我只是在故事板中使用了原型单元格,并将按钮直接挂钩到我的@IBOutlet
,完全避免了该单元格的NIB。
但所有这些都与您手头的主要问题无关:您可以根据需要创建自己的细胞。但希望这说明了一个基本的想法,即按下按钮,我没有尝试直接加载任何单元格,但我只是更新一些cellForRow
将用于知道要加载哪个单元格的状态变量然后告诉tableView
动画重新加载IndexPath
。
顺便说一句,我假设你的例子中第二行的两个不同的潜在单元需要不同的NIB。但是如果你没有,那就更容易了(只使用一个NIB和一个单元标识符),但想法是一样的,只需更新你的状态变量并用动画重新加载第二行。