gpt4 book ai didi

swift - 我如何制作一个喜欢按钮?

转载 作者:行者123 更新时间:2023-11-30 10:12:37 25 4
gpt4 key购买 nike

我正在使用 Swift 练习教程制作一个社交媒体应用程序克隆,但我想使用 Parse 添加一个“赞”按钮,但不确定如何操作。有谁知道教程或者您知道如何去做吗?我试图制作这样的一个,但我很确定它不正确......

我把它放在单元 View Controller 中

@IBAction func likeButton(sender: AnyObject) {

a++

var post = PFObject(className: "Post")

post["likes"] = a


}

}

这进入了 feed View Controller ...

var messages = [String]()
var usernames = [String]()
var imageFiles = [PFFile]()
var users = [String: String]()
var likesArray = [String]()

override func viewDidLoad() {
super.viewDidLoad()

var query = PFUser.query()

query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

if let users = objects {

self.messages.removeAll(keepCapacity: true)
self.users.removeAll(keepCapacity: true)
self.imageFiles.removeAll(keepCapacity: true)
self.usernames.removeAll(keepCapacity: true)
self.likesArray.removeAll(keepCapacity: true)

for object in users {

if let user = object as? PFUser {

self.users[user.objectId!] = user.username!
}

}
}
})

var getFollowedUsersQuery = PFQuery(className: "followers")

getFollowedUsersQuery.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)

getFollowedUsersQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

if let objects = objects {

for object in objects {

var followedUser = object["following"] as! String

var query = PFQuery(className: "Post")

query.whereKey("userId", equalTo: followedUser)

query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

if let objects = objects {

for object in objects {

self.messages.append(object["message"] as! String)

self.imageFiles.append(object["imageFile"] as! PFFile)

self.usernames.append(self.users[object["userId"] as! String]!)

self.likesArray.append(object["likes"] as? String)

self.tableView.reloadData()


}

}

})

}
}
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return usernames.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! cell

imageFiles[indexPath.row].getDataInBackgroundWithBlock { (data, error) -> Void in

if let downloadedImage = UIImage(data: data!) {

myCell.postedImage.image = downloadedImage
}

}


myCell.username.text = usernames[indexPath.row]

myCell.message.text = messages[indexPath.row]

myCell.likeLabel.text = likesArray[indexPath.row]

myCell.selectionStyle = .None

return myCell
}


}

这是发布图片和消息时的 View Controller ...

var likes = 0

var picSelected = false

@IBOutlet var feed: UIButton!

func displayAlert(title: String, message: String) {

var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (action) -> Void in


}))

presentViewController(alert, animated: true, completion: nil)

}


var activityIndicator = UIActivityIndicatorView()

@IBOutlet var imageToPost: UIImageView!

@IBAction func chooseImage(sender: AnyObject) {

var image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false

presentViewController(image, animated: true, completion: nil)

}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {

dismissViewControllerAnimated(true, completion: nil)

imageToPost.image = image

picSelected = true
}

@IBOutlet var message: UITextField!


@IBAction func postImage(sender: AnyObject) {

if message.text != "" && picSelected != false {
picSelected = false
activityIndicator = UIActivityIndicatorView(frame: view.frame)
activityIndicator.backgroundColor = UIColor(white: 1.0, alpha: 0.5)
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(activityIndicator)
activityIndicator.startAnimating()

UIApplication.sharedApplication().beginIgnoringInteractionEvents()

var post = PFObject(className: "Post")

post["message"] = message.text

post["userId"] = PFUser.currentUser()?.objectId!

let imageData = UIImagePNGRepresentation(imageToPost.image)

let imageFile = PFFile(name: "image.png", data: imageData)

post["imageFile"] = imageFile

post["likes"] = a

post.saveInBackgroundWithBlock { (success, error) -> Void in

self.activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()

if error == nil {

self.displayAlert("Image Posted!", message: "Your image has been posted successfully.")

self.imageToPost.image = UIImage(named: "Blank-Person1.png")

self.message.text = ""

}

}

} else {
displayAlert("Could Not Post Image!", message: "Please enter a message and/or select a picture.")
}

}
override func viewDidLoad() {
super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

最佳答案

我要制作这样的“赞”按钮:

  • 在解析中创建 Names 列(数组列)来保存点击 Like 按钮的姓名
  • 在解析中创建“Likes”列(整数列)以保存“Names”列的计数

当有人点击“喜欢”按钮时,将他的名字添加到解析中的名称数组中,如果他再次点击它,则删除他的名字

您需要获取 NamesArray 列中的所有名称并快速对它们进行计数,然后更新 Likes 列

我不确定,但这现在就在我的脑海里,但如果你想到它,你就会得到更好的方法。

关于swift - 我如何制作一个喜欢按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32161057/

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