Я пытаюсь реализовать подобную функцию в своем приложении, используя синтаксический анализ. Если пользователь нажал кнопку голосования. Ярлык также увеличивает количество одинаковых чисел на стороне анализа. Однако с моим кодом пользователь может многократно нажать, чтобы увеличить подобное. Я бы хотел, чтобы он обнаружил, что пользователь нажал и отключил подобную кнопку. Для этого я создал класс в разделе «Понравилось». Я сделал имя пользователя, imageId и строковый столбец, и подобныйStatus в качестве логического. Однако я не могу сделать так, что если пользователю понравится какое-либо изображение, он добавит к нему новый элемент с userId, ImageId и likeStatus. Это коллекция Просмотреть кодПроанализируйте, если пользователь уже нажал кнопку
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("newview", forIndexPath: indexPath) as! NewCollectionViewCell
let item = self.votes[indexPath.row]
// Display the country name
if let value = item["imageText"] as? String {
cell.postsLabel.text = value
}
// Display "initial" flag image
var initialThumbnail = UIImage(named: "question")
cell.postsImageView.image = initialThumbnail
cell.complition = {
self.likeButton(indexPath)
}
if let votesValue = item["votes"] as? Int
{
cell.votesLabel?.text = "\(votesValue)"
}
// Fetch final flag image - if it exists
if let value = item["imageFile"] as? PFFile {
cell.postsImageView.file = value
cell.postsImageView.loadInBackground({ (image: UIImage?, error: NSError?) -> Void in
if error != nil {
cell.postsImageView.image = image
}
})
}
return cell
}
/*
==========================================================================================
Segue methods
==========================================================================================
*/
func likeButton(indexPath:NSIndexPath)
{
let cell = self.collectionView.cellForItemAtIndexPath(indexPath) as! NewCollectionViewCell
let object = self.votes[indexPath.row]
if let likes = object["votes"] as? Int
{
object["votes"] = likes + 1
object.saveInBackgroundWithBlock{ (success:Bool,error:NSError?) -> Void in
println("Data saved")
}
cell.votesLabel?.text = "\(likes + 1)"
}
else
{
object["votes"] = 1
object.saveInBackgroundWithBlock{ (success:Bool,error:NSError?) -> Void in
println("Data saved")
}
cell.votesLabel?.text = "1"
}
}
и это код ячейки
@IBAction func vote(sender: AnyObject) {
if self.complition != nil
{
self.complition!()
}
}
}
Любые советы или Как я могу это сделать в коде? Спасибо.