0
所以我創建了一個應用程序,用戶可以上傳圖片和文字。這些帖子將顯示在UICollectionView中我將提供一些代碼供您在下面看到。查詢圖片和文字解析
所以我PostsViewController看起來是這樣的:
import UIKit
import Parse
import Bolts
import ActiveLabel
extension UILabel {
func setSizeFont (sizeFont: CGFloat) {
self.font = UIFont(name: self.font.fontName, size: sizeFont)!
self.sizeToFit()
}
}
extension NSDate {
var timeAgo: String {
let minute = 60
let hour = 60 * minute
let day = 24 * hour
let secondsAgo = Int(NSDate().timeIntervalSinceDate(self))
if secondsAgo < 0 { return "later" }
if secondsAgo == 0 { return "now" }
if secondsAgo == 1 { return "1 second ago" }
if secondsAgo < minute { return "\(secondsAgo) seconds ago" }
if secondsAgo < (2 * minute) { return "1 minute ago" }
if secondsAgo < hour { return "\(secondsAgo/minute) minutes ago" }
if secondsAgo < 2 * hour { return "1 hour ago" }
if secondsAgo < day { return "\(secondsAgo/hour) hours ago" }
let formatter = NSDateFormatter()
formatter.dateFormat = "M/d/yy"
return formatter.stringFromDate(self)
}
}
extension UIImage {
func makeImageWithColorAndSize(color: UIColor, size: CGSize) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(CGRectMake(0, 0, size.width, size.height))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
struct Details {
var username:String!
var text:String!
var CreatedAt:NSDate!
var image:String!
var objID:String!
var likedBy:NSArray
var comments:NSArray
init(username:String,text:String,CreatedAt:NSDate,image:String,objID:String,likedBy:NSArray,comments:NSArray){
self.username = username
self.text = text
self.CreatedAt = CreatedAt
self.image = image
self.objID = objID
self.likedBy = likedBy
self.comments = comments
}
}
class HomeViewController: UIViewController, UICollectionViewDelegate, PlayerDelegate {
@IBOutlet var collectionView: UICollectionView!
var arrayOfDetails = [Details]()
var likedPhotos = String()
let Like = UIImage(named: "Like.png")
let LikeDone = UIImage(named: "LikeDone.png")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.collectionView.delegate = self
queryCurrentUploads()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func queryCurrentUploads() {
self.arrayOfDetails.removeAll()
let query = PFQuery(className: "currentUploads")
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock { (objects:[PFObject]?, error: NSError?) -> Void in
if error == nil
{
if let newObjects = objects {
for oneobject in newObjects {
let text = oneobject["imageText"] as! String
let username = oneobject["username"] as! String
let objID = oneobject.objectId!
let time = oneobject.createdAt!
let likedBy = oneobject["likedBy"] as! NSArray
let comments = oneobject["comments"] as! NSArray
if let userImage = oneobject["imageFile"] as? PFFile {
let userImage = oneobject["imageFile"] as! PFFile
let imageURL = userImage.url
let OneBigObject = Details(username: username, text: text, CreatedAt: time, image: imageURL!, objID: objID, likedBy: likedBy, comments: comments)
self.arrayOfDetails.append(OneBigObject)
dispatch_async(dispatch_get_main_queue()) { self.collectionView.reloadData() }
}
}
}
}
}
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return self.arrayOfDetails.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let imageCell = collectionView.dequeueReusableCellWithReuseIdentifier("imageCell", forIndexPath: indexPath) as? CollectionViewCell
let post = self.arrayOfDetails[indexPath.row]
imageCell?.flagContentButton.titleLabel?.adjustsFontSizeToFitWidth = true
imageCell?.uploadedTimeLabel.adjustsFontSizeToFitWidth = true
imageCell?.commentsButton.titleLabel?.adjustsFontSizeToFitWidth = true
imageCell?.likeButton.enabled = true
if (self.arrayOfDetails.count > indexPath.row) {
imageCell?.imageText.text = post.text
imageCell?.imageText.numberOfLines = 0
imageCell?.imageText.verticalTextAlignmentCenter = true
imageCell?.imageText.minFontSize = 9
imageCell?.likeButton.setTitle(post.likedBy.count.description, forState: UIControlState.Normal)
imageCell?.commentsButton.setTitle("Comments(\(String(post.comments.count.description)))", forState: UIControlState.Normal)
imageCell?.objectID.append(post.objID)
imageCell?.uploadedTimeLabel.text = post.CreatedAt.timeAgo
if (post.likedBy.containsObject((PFUser.currentUser()?.username)!!)){
imageCell?.likeButton.setBackgroundImage(self.LikeDone, forState: UIControlState.Normal)
}
else{
imageCell?.likeButton.setBackgroundImage(self.Like, forState: UIControlState.Normal)
}
imageCell?.imageView.setImageWithUrl(NSURL(string: post.image)!, placeHolderImage: UIImage(named: "Placeholder"))
}
return imageCell!
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.width+140)
}
}
這是我CollectionViewCell.swift:
import UIKit
import Parse
import ActiveLabel
class CollectionViewCell: UICollectionViewCell {
@IBOutlet var imageView: UIImageView!
@IBOutlet var imageText: ActiveLabel!
@IBOutlet var uploadedTimeLabel: UILabel!
@IBOutlet var flagContentButton: UIButton!
@IBOutlet var likeButton: UIButton!
@IBOutlet var commentsButton: UIButton!
var objectID = [String]()
}
所以我的問題是:有沒有更快或更簡單/容易的方法從Parse.com和我的UICollectionView獲取數據?
好吧,我試圖修改自己的代碼,看看這裏:http://pastebin.com/Ayvxbu2x - 請告訴我你的想法的它現在。 –
這裏有幾個編輯和一些解釋http://pastebin.com/ZMD0iT9L –
請檢查我的[項目](https://github.com/wagnercasey/SightCR)我在Github上,它非常類似於你是做。 –