我尝试使用自定义单元格创建表格。每次单击时,自定义单元格样式都会更改,就像内容正在更改位置一样。我设置了约束,我尝试过使用
tableView.deselectRow(at: indexPath, animated: true)
但我得到了相同的结果! 这就是我所说的:https://i.imgur.com/bSlTHnO.gifv
我的故事板如下所示:https://i.imgur.com/gwTWaxV.png
标签限制:https://imgur.com/a/urOtx
这是我的FeedViewController代码:
import Foundation
import UIKit
import Firebase
class FeedViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var date: String?
var timeStamp : String?
var ref: DatabaseReference!
var refHandle: UInt!
var id: String?
var postList = [Post]()
let cellReuseIdentifier = "cell"
@IBOutlet var postTableView: UITableView?
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
fetchPosts()
postTableView?.delegate = self
postTableView?.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: NewsFeedCustomCell = postTableView?.dequeueReusableCell(withIdentifier: "feedCell", for: indexPath) as! NewsFeedCustomCell
let posts: Post = postList[indexPath.row]
cell.nameLabel.text = posts.name
cell.descriptionLabel.text = posts.info
cell.titleLabel.text = posts.title
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You tapped cell number: \(indexPath)")
tableView.deselectRow(at: indexPath, animated: true)
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 44.0
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func fetchPosts() {
ref.child("posts").queryOrderedByKey().observe(DataEventType.value) { (snapshot) in
if snapshot.childrenCount > 0 {
self.postList.removeAll()
for childSnap in snapshot.children.allObjects {
let snap = childSnap as! DataSnapshot
let value = snap.value as? NSDictionary
let title = value?["title"] as? String
let time = value?["time"] as? String
let privacy = value?["privacy"] as? Int
let postDate = value?["postDate"] as? String
let description = value?["description"] as? String
let likesNr = value?["likesNr"] as? Int
let commentsNr = value?["commentsNr"] as? Int
let activities = value?["activities"] as? String
let location = value?["location"] as? String
let post = Post(title: title, name: "", image: #imageLiteral(resourceName: "Post"), info: description, likesNr: likesNr, privacy: privacy, location: location, commentsNr: commentsNr, activities: activities, date: postDate)
self.postList.append(post)
self.postTableView?.reloadData()
}
}
}
}
}