gpt4 book ai didi

ios - 使用 Parse 在 Swift 中实现相似和不同的功能

转载 作者:搜寻专家 更新时间:2023-11-01 05:44:12 25 4
gpt4 key购买 nike

我正在尝试使用 Swift 2.0、Storyboard 和 Parse 在我的 iOS 应用程序中实现一个喜欢/不喜欢的功能,用户可以在其中喜欢/不喜欢其他用户或他们自己创建的帖子 - 就像 Instagram、Facebook 和其他社交应用程序一样。

我在 Storyboard 中有一个按钮连接到一个名为 likeButtonIBOutlet 和一个名为 likeButtonTappedIBAction .

我相信 cellForRowAtIndexPath 方法也与正确实现此功能有关。

我认为我对下面代码的注释中需要发生的事情有正确的想法,但是,我不知道如何检查特定帖子是否被喜欢。 我如何检查帖子是否被点赞,以便我可以切换likeButton 图像、递增/递减likeCount,以及添加/删除当前用户和用户喜欢的帖子之间的关系。

此外,我是否采用了“正确的”(传统的)方法来处理标准的相似/不同特征?我很想听听您的反馈。感谢您的宝贵时间和帮助!

class TimelineFeedViewController: PFQueryTableViewController {
var userLike = PFUser.currentUser()?.relationForKey("likes")

@IBAction func likeButtonTapped(sender: UIButton) {
// The following code has errors - for example, `object` is an unresolved
// identifier (it's supposed to be a PFObject that holds a post at a specific
// indexPath)
// Also, I cant access the likeButton for some reason. Only when I do
// cell.likeButton in `cellForRowAtIndexPath`.
// If the button is liked already (it's filled)
// Toggle likeButton image to UNfilled version
// "if likeButton isLiked == true" below is pseudocode of what I am trying to do
if likeButton isLiked == true {
let image = UIImage(named: "likeButtonUnfilled")
cell.likeButton.setImage (image, forState: UIControlState)

// Decrement the likeCount
object!.decrementKey("count")

// Remove the relation bet user and post
self.userLike?.removeObject(object!)
} else {
// the button is NOT liked already (it's not filled)
// toggle the image to the filled version
let image = UIImage(named: "likeButton")
cell.likeButton.setImage (image, forState: UIControlState)

// Increment the likeCount
object!.incrementKey("count")

// Add the relation bet. user and post
self.userLike?.addObject(object!)
}

object!.saveIngBackground()
PFUser.currentUser()?.saveInBackground()

self.tableView.reloadData()
}
}

最佳答案

假设您有自定义的 UITableViewCell 类并从 Parse 中获取数据,而不是在您的 UITableView 数据源方法中

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("skillsCell", forIndexPath: indexPath) as! YOURTableViewCell
//Check If item is liked
if (isLiked) {

//Set image for like on button
}
else {
//Set image for unlike on button
}
cell.YourButton.tag = indexPath.row // This will assign tag to each button in tableView

return cell
}

比在 YourViewController 中添加 UIButton Action

@IBAction func testButtonAction(sender: UIButton) {

print(sender.tag)

let cell = testTableView.cellForRowAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: 0)) as! TestTableViewCell

if cell.likeButton.titleLabel?.text == "Cell \(sender.tag)" { //Your logic here. Check If button's image has "Liked Image than change Image to UnLiked Image"
cell.likeButton.text = "\(sender.tag)"
}
else {
cell.likeButton.text = "Cell \(sender.tag)"
}
}

这将为 UITableView 中的按钮实现 Like/Unlike。还要确保相应地更新 Parse 中的类。

关于ios - 使用 Parse 在 Swift 中实现相似和不同的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34083763/

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