我所有的代码和记录都按照我想要的方式设置!您可以创建一个帖子,并将帖子保存在cloudKit上的记录中。问题是我只想加载过去72小时内制作的数据/帖子。
如何在我的代码中实现它?这是tableView的所有代码:
class SweetsTableViewController: UITableViewController {
var sweets = [CKRecord]()
var refresh:UIRefreshControl!
override func viewDidLoad() {
super.viewDidLoad()
refresh = UIRefreshControl()
refresh.attributedTitle = NSAttributedString(string: "Pull to Refresh Page")
refresh.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
self.tableView.addSubview(refresh)
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 1000.0
loadData()
}
func loadData () {
sweets = [CKRecord]()
let publicData = CKContainer.defaultContainer().publicCloudDatabase
let query = CKQuery(recordType: "Sweet", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
publicData.performQuery(query, inZoneWithID: nil) { (results:[CKRecord]?, error:NSError?) -> Void in
if let sweets = results {
self.sweets = sweets
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 1000.0
self.refresh.endRefreshing()
})
}
}
}
@IBAction func sendSweet(sender: AnyObject) {
let alert = UIAlertController(title: "New Post", message: "Enter your Post", preferredStyle: .Alert)
alert.addTextFieldWithConfigurationHandler { (textField:UITextField) -> Void in
textField.placeholder = "Your Post"
}
alert.addAction(UIAlertAction(title: "Send", style: .Default, handler: { (action:UIAlertAction) -> Void in
let textField = alert.textFields!.first!
let defaults = NSUserDefaults.standardUserDefaults()
let label = defaults.objectForKey("label") as? String
if textField.text != "" {
let newSweet = CKRecord(recordType: "Sweet")
newSweet["content"] = textField.text
newSweet["label"] = label
let publicData = CKContainer.defaultContainer().publicCloudDatabase
publicData.saveRecord(newSweet, completionHandler: { (record:CKRecord?, error:NSError?) -> Void in
if error == nil {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.beginUpdates()
self.sweets.insert(newSweet, atIndex: 0)
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Top)
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 1000.0
self.tableView.endUpdates()
})
}else{
print(error)
}
})
}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sweets.count
}
func configureTableView() {
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 1000.0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 1000.0
cell.textLabel?.sizeToFit()
cell.textLabel?.numberOfLines = 0
if sweets.count == 0 {
return cell
}
let sweet = sweets[indexPath.row]
if let sweetContent = sweet["content"] as? String {
let label = sweet["label"] as? String
let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "- h:mm a"
let dateString = dateFormat.stringFromDate(sweet.creationDate!)
cell.textLabel?.text = sweetContent
cell.detailTextLabel?.text = label
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 1000.0
}
return cell
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let sweet = sweets[indexPath.row]
if let sweetContent = sweet["content"] as? String {
let label = sweet["label"] as? String
let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "h:mm a"
let dateString = dateFormat.stringFromDate(sweet.creationDate!)
var timeAction = UITableViewRowAction(style: .Normal, title: "\(dateString)") { (action: UITableViewRowAction, indexPath: NSIndexPath!) -> Void in
let firstActivityItem = self.sweets[indexPath.row]
let activityViewController = UIActivityViewController(activityItems: [firstActivityItem], applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)
}
timeAction.backgroundColor = UIColor.grayColor()
return [timeAction]
}
return nil}
}
答案 0 :(得分:0)
首先将modificationDate
标记为可查询。
Date Modified
在图片中。然后使用modificationDate
NSPredicate
设置日期限制
let publicData = CKContainer.defaultContainer().publicCloudDatabase
let date = now.dateByAddingTimeInterval(-60*60*24*3)
let predicate = NSPredicate(format: "modificationDate > %@", date)
let query = CKQuery(recordType: "Sweet", predicate: predicate)