我有一个UItableview单元格,我从firebase获取其数据。我有两个问题:
我正在执行以下代码,但仍然无法理解该行为。我甚至尝试删除单元子视图,但这并不是在表格中加载单元格。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let post = posts[indexPath.row]
if let cell = tableView.dequeueReusableCell(withIdentifier: "FeedCell") as? FeedCell {
cell.caption.text = nil
cell.configureCell(post: post)// sets the caption text here!
cell.caption.sizeToFit()
cell.delegate = self
return cell
} else {
return FeedCell()
}
}
每次看到一个单元格时,我都在为单元格设置动画。但每当我执行它时,动画都适用于所有细胞。有什么可能的解决方案?代码:
class FeedCell: UITableViewCell {
@IBOutlet weak var caption: UILabel!
@IBOutlet weak var activityindicator: NVActivityIndicatorView!
var usersender: User!
var posts: Posts!
var viewRef: DatabaseReference!
var users: User!
var delegate: FeedCellDelegate!
override func layoutSubviews() {
super.layoutSubviews()
contentView.layoutIfNeeded()
}
func setDelegate(delegate: FeedCellDelegate) {
self.delegate = delegate
}
override func awakeFromNib() {
super.awakeFromNib()
NotificationCenter.default.addObserver(self, selector: #selector(animateIndicator), name: .animateIndicator, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(stopIndicator), name: .stopIndicator, object: nil)
emotionDisplay.alpha = 0.0
caption.addDropShadow(opacity: 3, radius: 8)
}
@objc func animateIndicator(note: Notification){
activityindicator.startAnimating()
}
@objc func stopIndicator(note: Notification){
activityindicator.stopAnimating()
}
@IBAction func gotoprof(_ sender: Any) {
self.delegate.segueToUser(user: usersender)
}
func configureCell(post: Posts, img: UIImage? = nil) {
self.posts = post
self.caption.text = posts.caption
}
}
请指出正确的方向。
答案 0 :(得分:0)
就像@ Tj3n写的那样 - 所有细胞都观察到相同的通知。
最简单的方法是使用您在FeedCell类中编写的代码为要创建动画的单元格创建第二个类。
对于其他单元格,只需从awakeFromNib()中删除:
NotificationCenter.default.addObserver(self, selector: #selector(animateIndicator), name: .animateIndicator, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(stopIndicator), name: .stopIndicator, object: nil)
awakeFromNib()看起来应该是:
override func awakeFromNib() {
super.awakeFromNib()
caption.addDropShadow(opacity: 3, radius: 8)
}
答案 1 :(得分:0)
在函数 tableView(cellForRowAt)中,编写如下代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let post = posts[indexPath.row]
let currentCell = tableView.dequeueReusableCell(withIdentifier: "FeedCell", for: indexPath) as! FeedCell
...
return currentCell
}