I have implemented a vote feature in my app so that user can vote
up a post. I am using a collection view
and have put a label
and a button
. When the button is pressed the vote counts up. I have that code in the cell view controller
. This is my collection view controller and even though I have said that votes in the cell I have an error saying
use of unresolved identifier votes
Stuck for many hours. Any help is appreciated..Thank you. In the cellForItemAtIndexPath
I have
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("newview", forIndexPath: indexPath) as! NewCollectionViewCell
//cell.backgroundColor = UIColor.blueColor()
// Display the country name
if let value = posts[indexPath.row]["imageText"] as? String {
cell.postsLabel.text = value
println("it should be there")
}
// Display "initial" flag image
var initialThumbnail = UIImage(named: "question")
cell.postsImageView.image = initialThumbnail
// Fetch final flag image - if it exists
if let value = posts[indexPath.row]["imageFile"] as? PFFile {
cell.postsImageView.file = value
cell.postsImageView.loadInBackground({ (image: UIImage?, error: NSError?) -> Void in
if error != nil {
}
})
// let finalImage = tops[indexPath.row]["tops"] as? PFFile
}
I get an error from this code.
if let value = posts[indexPath.row]["votes"] as? Int {
if votes == nil {
votes = 0}
cell.votesLabel?.text = "\(votes!) votes"
}
return cell
}
Also in my cell view I also have
@IBAction func vote(sender: AnyObject) {
if (parseObject != nil) {
if var votes:Int? = parseObject!.objectForKey("votes") as? Int {
votes!++
parseObject!.setObject(votes!, forKey: "votes")
parseObject!.saveInBackgroundWithTarget(nil, selector: nil)
votesLabel?.text = "\(votes!) votes"
}
}}}
Try replacing below lines of code
if let value = posts[indexPath.row]["votes"] as? Int {
if votes == nil {
votes = 0}
}
with
if let votes = posts[indexPath.row]["votes"] as? Int {
if votes == nil {
votes = 0}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.