gpt4 book ai didi

ios - 如何调用不带参数的函数来更新界面?

转载 作者:行者123 更新时间:2023-11-29 01:31:36 25 4
gpt4 key购买 nike

我想用我拥有的模型更新我的界面。我的 PFTableViewCell 中有这两个导出:

class UserFeedCell: PFTableViewCell {

@IBOutlet weak var likeButton: UIButton!
@IBOutlet weak var dislikeButton: UIButton!

我想用以下代码更新这两个导出按钮:

var vote: Int = 0 // initialize to user's existing vote, retrieved from the server
var likeCount: Int = 0 // initialize to existing like count, retrieved from the server
var dislikeCount: Int = 0 // initialize to existing dislike count, retrieved from the server

@IBAction func dislikeButton(sender: UIButton) {
buttonWasClickedForVote(-1)
print(likeCount)
print(dislikeCount)
}

@IBAction func likeButton(sender: UIButton) {
buttonWasClickedForVote(1)
print(likeCount)
print(dislikeCount)
}

private func buttonWasClickedForVote(buttonVote: Int) {
if buttonVote == vote {
// User wants to undo her existing vote.
applyChange(-1, toCountForVote: vote)
vote = 0
}

else {
// User wants to force vote to toggledVote.
// Undo current vote, if any.
applyChange(-1, toCountForVote: vote)

// Apply new vote.
vote = buttonVote
applyChange(1, toCountForVote: vote)
}
}

private func applyChange(change: Int, toCountForVote vote: Int ) {
if vote == -1 { dislikeCount += change }
else if vote == 1 { likeCount += change }
}

private func updateUserInterfaceFromModel() {
let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as! UserFeedCell
cell.likeButton.selected = vote == 1
cell.dislikeButton.selected = vote == -1
cell.likeButton.setTitle("\(likeCount)", forState: .Normal)
cell.dislikeButton.setTitle("\(dislikeCount)", forState: .Normal)
}

如何更新我的网点?我尝试调用 viewDidLoad,但我的 socket 没有更新。

最佳答案

你可以这样使用:-

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "TableViewCell" // your cell identifier name in storyboard
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! PFTableViewCell
cell.likeButton.selected = vote == 1
cell.dislikeButton.selected = vote == -1
cell.likeButton.titleLabel!.text = "\(likeCount)"
cell.dislikeButton.titleLabel!.text = "\(dislikeCount)"
return cell
}

问题更新后,您可以使用 awakeFromNib 来完成此任务...

class UserFeedCell: PFTableViewCell {

@IBOutlet weak var likeButton: UIButton!
@IBOutlet weak var dislikeButton: UIButton!

override func awakeFromNib() {
super.awakeFromNib()
likeButton.titleLabel!.text = "\(likeCount)"
dislikeButton.titleLabel!.text = "\(dislikeCount)"
}

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

关于ios - 如何调用不带参数的函数来更新界面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33405527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com