[英]TableView cells are repeating Swift
So I'm making a GET request to my database and then adding each element from my array into a ViewModel, in var mainComment: Comments?
所以我正在向我的数据库发出 GET 请求,然后将数组中的每个元素添加到 ViewModel 中,在
var mainComment: Comments?
. . In my Custom Cell I'm assigning my label text to the ViewModel's
mainComment.likes
.在我的自定义单元格中,我将我的 label 文本分配给 ViewModel 的
mainComment.likes
。 I'm supposed to get one like for only my one cell but instead the one like is repeating for multiple cells.我应该只为我的一个单元格获得一个类似的,但相反,一个类似的是为多个单元格重复。 https://giphy.com/gifs/L2U6FALmxWx981nO44
https://giphy.com/gifs/L2U6FALmxWx981nO44
struct Comments: Codable {
var id:String?
var likes:Int?
}
import UIKit
import Foundation
class CommentViewModel {
var mainComment: Comments?
}
//CommentVC
var comments:[CommentViewModel] = []
func loadComments(completion:@escaping(()->())) {
guard let post_id = post_id else {
return
}
let getComments = GETComments(id: post_id, path: "comments")
getComments.getAllById { comments in
self.comments = comments.map { comment in
let ret = CommentViewModel()
ret.mainComment = comment
return ret
}
self.myTableView.reloadData()
completion()
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath) as! CommentCell
let item = comments[indexPath.item]
cell.viewModel = item
return cell
}
class CommentCell {
var viewModel: CommentViewModel? {
didSet {
if let item = viewModel {
if let numberOfLikes = item.mainComment?.likes {
self.numberOfLikes.text = "\(numberOfLikes)"
}
}
}
lazy var numberOfLikes:UILabel = {
let label = UILabel()
label.textAlignment = .center
label.text = "0"
return label
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(numberOfLikes)
commentLikesConstraints()
}
func commentLikesConstraints() {
numberOfLikes.translatesAutoresizingMaskIntoConstraints = false
numberOfLikes.widthAnchor.constraint(equalToConstant: 60).isActive = true
numberOfLikes.heightAnchor.constraint(equalToConstant: 30).isActive = true
numberOfLikes.centerXAnchor.constraint(equalTo: likeBtn.centerXAnchor).isActive = true
numberOfLikes.topAnchor.constraint(equalTo: likeBtn.bottomAnchor, constant: -5).isActive = true
}
Since you're using mainComment
and likes
as optionals:由于您使用的是
mainComment
和likes
作为可选项:
struct Comments: Codable {
var id:String?
var likes:Int?
}
You need to handle an else
case (and probably also want an else
case if viewModel
is nil):您需要处理一个
else
案例(如果viewModel
为 nil,可能还需要一个else
案例):
var viewModel: CommentViewModel? {
didSet {
if let item = viewModel {
if let numberOfLikes = item.mainComment?.likes {
self.numberOfLikes.text = "\(numberOfLikes)"
} else {
self.numberOfLikes.text = "0"
}
} else {
// do what you need to do if viewModel is nil
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.