对Swift来说很新,但是我想做一个控制器的演示,当我打电话给那个控制器时,我没有附上textLabel的文字。当我第一次运行应用程序时,我做对了,但是当我尝试添加新的评论时,它不起作用。 这是我的代码
override func viewDidLoad() {
super.viewDidLoad()
observeReviews()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Sign Out", style: .plain, target: self, action: #selector(handleLogout))
let newReview = UIBarButtonItem(barButtonSystemItem: .compose, target: self, action: #selector(handleNewReview))
let addCategory = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(handleNewCategory))
navigationItem.rightBarButtonItem = addCategory
navigationItem.rightBarButtonItems?.append(newReview)
tableView.register(ReviewCell.self, forCellReuseIdentifier: cellId)
checkIfUserIsLoggedIn()
}
func observeReviews() {
let ref = Database.database().reference().child("reviews").queryOrdered(byChild: "timestamp")
ref.observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String:AnyObject] {
let review = Review()
review.gameId = dictionary["gameId"] as? String
review.userId = dictionary["userId"] as? String
review.reviewText = dictionary["reviewText"] as? String
review.rate = dictionary["rate"] as? Int
review.timestamp = dictionary["timestamp"] as? Int
self.reviews.append(review)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}, withCancel: nil)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! ReviewCell
let review = reviews[indexPath.row]
cell.review = review
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 72
}
}
class ReviewCell: UITableViewCell {
var review: Review? {
didSet {
var rate = ""
if let rating = review?.rate {
switch rating {
case 0:
rate = "It's awful"
break
case 1:
rate = "Nothing special"
break
case 2:
rate = "Meh"
break
case 3:
rate = "Maybe I'll play it if I don't have Internet"
break
case 4:
rate = "IT'S AWESOME"
break
default:
rate = "none"
break
}
}
let userId = review?.userId
let refUsers = Database.database().reference().child("users").child(userId!)
refUsers.observeSingleEvent(of: .value, with: { (snapUsers) in
if let dictUsers = snapUsers.value as? [String: AnyObject] {
let user = User()
user.username = dictUsers["username"] as? String
user.email = dictUsers["email"] as? String
user.profilePicture = dictUsers["profileImageUrl"] as? String
self.textLabel?.text = user.username! + " rated "
}
}, withCancel: nil)
let gameId = review!.gameId
let refGames = Database.database().reference().child("games").child(gameId!)
refGames.observeSingleEvent(of: .value, with: { (snapGames) in
if let dictGames = snapGames.value as? [String: AnyObject] {
let game = Game()
game.gid = gameId
game.name = dictGames["name"] as? String
game.desc = dictGames["desc"] as? String
game.coverPhoto = dictGames["gameCover"] as? String
self.textLabel?.text?.append(game.name! + " as " + "\"" + rate + "\"")
if let gameCoverUrl = game.coverPhoto {
self.gameCoverView.loadImageWithCacheFromUrlString(urlString: gameCoverUrl)
}
}
}, withCancel: nil)
self.detailTextLabel?.text = "Tap to see more"
}
}
override func prepareForReuse() {
print(textLabel?.text)
}
override func layoutSubviews() {
super.layoutSubviews()
textLabel?.frame = CGRect(x: 64, y: textLabel!.frame.origin.y - 2, width: textLabel!.frame.width, height: textLabel!.frame.height)
detailTextLabel?.frame = CGRect(x: 64, y: detailTextLabel!.frame.origin.y + 2, width: detailTextLabel!.frame.width, height: detailTextLabel!.frame.height)
}
let gameCoverView: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
return imageView
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
addSubview(gameCoverView)
//x,y,width,height constraints
gameCoverView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 8).isActive = true
gameCoverView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
gameCoverView.widthAnchor.constraint(equalToConstant: 48).isActive = true
gameCoverView.heightAnchor.constraint(equalToConstant: 48).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
所以,在viewDidLoad()
我正在调用observeReviews()
从Firebase获取评论,然后当我填充tableview
时,我正在使用特殊类来获取该单元格。所以,当我第一次运行应用程序时,我得到了所有的东西,但是当我为某些游戏添加新的评论时,从该视图控制器我呈现这个视图控制器,而我得到的只是第一个,例如:
正确的一个:弗拉基米尔认为CandyCrushSaga很棒
我得到的:弗拉基米尔评价
答案 0 :(得分:0)
而不是self.textLabel?.text?.append(game.name! + " as " + "\"" + rate + "\"")
尝试这个:
self.textLabel?.text = "\(game.name!) as \(your variable) rate \(your variable)"
或
self.textLabel?.text = self.textLabel?.text + "Text \(your variable)"