gpt4 book ai didi

ios - 将数据从 cloudkit 加载到 tableView 单元格中,但仅在过去 72 小时内(快速)

转载 作者:行者123 更新时间:2023-11-30 13:36:51 28 4
gpt4 key购买 nike

我也按照我想要的方式设置了所有代码和记录!您可以创建帖子,它会将帖子保存在 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}

}

最佳答案

首先将您的modificationDate标记为可查询。

图片中的

修改日期

enter image description here

然后使用 NSPredicate 设置 modificationDate 的日期限制

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)

关于ios - 将数据从 cloudkit 加载到 tableView 单元格中,但仅在过去 72 小时内(快速),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35966848/

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