[英]How to call a function with no parameters to update interface?
我想更新我的界面與模型我have.I在我的這兩個出口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
調用,但是我的商店沒有更新。
您可以這樣使用:-
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
}
}
viewDidLoad
不是在UITableViewCell
上設置屬性的正確位置。 您必須等待iOS詢問單元格,然后才能在單元格上設置屬性並返回它。 cellForRowAtIndexPath
是正確的方法。 您還需要確保正確設置了正確的屬性(使用indexPath.row
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.