Ios 使用Parse在Swift中实现相似和不相似功能
我正在尝试使用Swift 2.0、Storyboard和Parse在我的iOS应用程序中实现一个相似/不同的功能,用户可以在其中喜欢/不喜欢其他用户或自己创建的帖子,就像Instagram、Facebook和其他社交应用程序一样 我在故事板上有一个按钮连接到一个叫做likeButton的iButton插座和一个叫做likeButtonTapped的iBuction 我相信cellForRowAtIndexPath方法也能正确实现此功能 我认为我对下面代码的注释中需要发生的事情有正确的想法,但是,我不知道如何检查某个特定的帖子是否受欢迎。如何检查帖子是否受欢迎,以便切换likeButton图像,增加/减少likeCount,以及添加/删除当前用户和用户喜欢的帖子之间的关系 另外,对于标准的相似/不同功能,我是否采用了正确的常规方法?我很想听到你的反馈。谢谢你的时间和帮助Ios 使用Parse在Swift中实现相似和不相似功能,ios,swift,parse-platform,social-media-like,Ios,Swift,Parse Platform,Social Media Like,我正在尝试使用Swift 2.0、Storyboard和Parse在我的iOS应用程序中实现一个相似/不同的功能,用户可以在其中喜欢/不喜欢其他用户或自己创建的帖子,就像Instagram、Facebook和其他社交应用程序一样 我在故事板上有一个按钮连接到一个叫做likeButton的iButton插座和一个叫做likeButtonTapped的iBuction 我相信cellForRowAtIndexPath方法也能正确实现此功能 我认为我对下面代码的注释中需要发生的事情有正确的想法,但是,
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类,并且已经从解析中获取了数据,那么在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
}
而不是在ViewController中添加UIButton操作
@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中实现类似/不同的for按钮。还要确保在Parse中相应地更新类 假设您有自定义的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
}
而不是在ViewController中添加UIButton操作
@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中实现类似/不同的for按钮。还要确保在Parse中相应地更新类 在UITableViewCell中有按钮???@SaqibOmer我在一个单独的swift类文件中实现了IBoutlet,比如likeButton等,UITableViewCellIncrement和Decreation子类通常最好在云代码中实现。在关系中存储like可以。@Wain我的递增和递减代码行,即:。incrementKeycount正在更新类似于Parse cloud中count的count列…@Wain另外,当我尝试打印出关系userLike时,我会得到类似于:userLike:Optional PostClass>。。。在Parse中,当我单击User类的repress列中的一个关系时,它会显示该用户喜欢的所有帖子。现在我不确定如何在代码ie:中访问此列表,以便在检查喜欢或不喜欢哪些帖子时在if/else控制块中使用它。在UITableViewCell中有按钮???@SaqibOmer我实现了类似likeButton的IBOutlets,etc在一个单独的swift类文件中,其中UITableViewCellIncrement和Decreation子类通常最好在云代码中完成。在关系中存储like可以。@Wain我的递增和递减代码行,即:。incrementKeycount正在更新类似于Parse cloud中count的count列…@Wain另外,当我尝试打印出关系userLike时,我会得到类似于:userLike:Optional PostClass>。。。在Parse中,当我单击User类的repress列中的一个关系时,它会显示该用户喜欢的所有帖子。现在我不确定如何在代码ie:中访问此列表,以便在检查喜欢或不喜欢哪些帖子时在if/else控制块中使用它。谢谢Saqib。我在Parse中创建了一个类似的类。见更新的问题。但也有几个问题:1。如果是岛上的话,岛在哪里?我不能用这种方式访问解析。2.我不确定您何时在ViewController中说“添加UIButton”操作,如果您是指在另一个文件中。我的iAction如ButtontApped和CellForRowatineXpath方法在同一个文件/类中实现:类TimelineFeedViewController:PFQueryTableViewController。3 cell.likeButton.text=\sender.tag在if和else语句中给了我此错误:“UIButton”类型的值没有成员“text”。3,继续,我没有尝试更改likeButton的文本,只是更改likeButton的图像。我感谢你澄清你对这三个问题的答案!谢谢Saqib。我在Parse中创建了一个类似的类。见更新的问题。但也有几个问题:1。如果是岛上的话,岛在哪里?我不能用这种方式访问解析。2.我不确定您何时在ViewController中说“添加UIButton”操作,如果您是指在另一个文件中。我的iAction如ButtontApped和CellForRowatineXpath方法在同一个文件/类中实现:类TimelineFeedViewController:PFQueryTableViewController。3 cell.likeButton.text=\sender.tag在if和else语句中给了我此错误:“UIButton”类型的值没有成员“text”。3,继续,我没有尝试更改likeButton的文本,只是更改likeButton的图像。我感谢你澄清你对这三个问题的答案!