博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
27_Blog Reader
阅读量:6813 次
发布时间:2019-06-26

本文共 13532 字,大约阅读时间需要 45 分钟。

这个App是用来读取  的内容,然后显示出来。

用了新建工程时用了 Master-Detail Application 这个模板。用了Core Data用来存储内容,不过每次启动App还是会删除内容,然后重新下载最新的20条。所以如果没有网络的时候打开不显示内容。

在原工程的基础上,我在Core Data中增加了date这项,为了排列时按照时间排列。获取的内容中有published和updated两个时间,用published好一些。这里只简单的转了String而不是Date类型。

////  MasterViewController.swift//  Blog Reader////  Created by zcdll on 16/1/24.//  Copyright © 2016年 ZC. All rights reserved.//import UIKitimport CoreDataclass MasterViewController: UITableViewController, NSFetchedResultsControllerDelegate {    var detailViewController: DetailViewController? = nil    var managedObjectContext: NSManagedObjectContext? = nil    override func viewDidLoad() {        super.viewDidLoad()                let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate                let context: NSManagedObjectContext = appDel.managedObjectContext                let url = NSURL(string: "https://www.googleapis.com/blogger/v3/blogs/10861780/posts?key=AIzaSyCxL3Iltcd-oLc-dFUtdDG9TTuJGWilsYw")!                let session = NSURLSession.sharedSession()                let task = session.dataTaskWithURL(url) { (data, response, error) -> Void in                        if error != nil {                                print(error)                            } else {                                if let data = data {                                    //print(NSString(data: data, encoding: NSUTF8StringEncoding))                                        do {                                                let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary                                                if jsonResult.count > 0 {                                                        if let items = jsonResult["items"] as? NSArray {                                                                let request = NSFetchRequest(entityName: "BlogItems")                                                                request.returnsObjectsAsFaults = false                                                                do {                                                                        let results = try context.executeFetchRequest(request)                                                                        if results.count > 0 {                                                                                for result in results {                                                                                        context.deleteObject(result as! NSManagedObject)                                                                                        do { try context.save() } catch {}                                                                                    }                                                                            }                                                                    } catch {                                                                        print("Context fetched failed")                                                                    }                                                                for item in items {                                                                        if let title = item["title"] as? String {                                                                                if let content = item["content"] as? String {                                                                                        if let date = item["published"] as? String {                                                                                            let updatedDate = date.componentsSeparatedByString("-08:00")[0]                                                                                                print(updatedDate)                                                                                                //print(title)                                                //print(content)                                                                                                let newPost: NSManagedObject = NSEntityDescription.insertNewObjectForEntityForName("BlogItems", inManagedObjectContext: context)                                                                                                newPost.setValue(title, forKey: "title")                                                newPost.setValue(content, forKey: "content")                                                newPost.setValue(updatedDate, forKey: "date")                                                                                            }                                                                                    }                                                                            }                                                                        //print(items)                                }                                                            }                                                    }                                            } catch {}                                }                            }                    }                task.resume()                // Do any additional setup after loading the view, typically from a nib.        if let split = self.splitViewController {            let controllers = split.viewControllers            self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController        }    }    override func viewWillAppear(animated: Bool) {        self.clearsSelectionOnViewWillAppear = self.splitViewController!.collapsed        super.viewWillAppear(animated)    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }    // MARK: - Segues    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {        if segue.identifier == "showDetail" {            if let indexPath = self.tableView.indexPathForSelectedRow {            let object = self.fetchedResultsController.objectAtIndexPath(indexPath)                let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController                controller.detailItem = object                controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()                controller.navigationItem.leftItemsSupplementBackButton = true            }        }    }    // MARK: - Table View    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {        return 1    }    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        let sectionInfo = self.fetchedResultsController.sections![section]        return sectionInfo.numberOfObjects    }    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)        self.configureCell(cell, atIndexPath: indexPath)        return cell    }    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {        // Return false if you do not want the specified item to be editable.        return true    }    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {        if editingStyle == .Delete {            let context = self.fetchedResultsController.managedObjectContext            context.deleteObject(self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject)                            do {                try context.save()            } catch {                // Replace this implementation with code to handle the error appropriately.                // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.                //print("Unresolved error \(error), \(error.userInfo)")                abort()            }        }    }    func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {        let object = self.fetchedResultsController.objectAtIndexPath(indexPath)        cell.textLabel!.text = object.valueForKey("title")!.description    }    // MARK: - Fetched results controller    var fetchedResultsController: NSFetchedResultsController {        if _fetchedResultsController != nil {            return _fetchedResultsController!        }                let fetchRequest = NSFetchRequest()        // Edit the entity name as appropriate.        let entity = NSEntityDescription.entityForName("BlogItems", inManagedObjectContext: self.managedObjectContext!)        fetchRequest.entity = entity                // Set the batch size to a suitable number.        fetchRequest.fetchBatchSize = 20                // Edit the sort key as appropriate.        let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)                fetchRequest.sortDescriptors = [sortDescriptor]                // Edit the section name key path and cache name if appropriate.        // nil for section name key path means "no sections".        let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: "Master")        aFetchedResultsController.delegate = self        _fetchedResultsController = aFetchedResultsController                do {            try _fetchedResultsController!.performFetch()        } catch {             // Replace this implementation with code to handle the error appropriately.             // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.              //print("Unresolved error \(error), \(error.userInfo)")             abort()        }                return _fetchedResultsController!    }        var _fetchedResultsController: NSFetchedResultsController? = nil    func controllerWillChangeContent(controller: NSFetchedResultsController) {        self.tableView.beginUpdates()    }    func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {        switch type {            case .Insert:                self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)            case .Delete:                self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)            default:                return        }    }    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {        switch type {            case .Insert:                tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)            case .Delete:                tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)            case .Update:                self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)            case .Move:                tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)                tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)        }    }    func controllerDidChangeContent(controller: NSFetchedResultsController) {        self.tableView.endUpdates()    }    /*     // Implementing the above methods to update the table view in response to individual changes may have performance implications if a large number of changes are made simultaneously. If this proves to be an issue, you can instead just implement controllerDidChangeContent: which notifies the delegate that all section and object changes have been processed.          func controllerDidChangeContent(controller: NSFetchedResultsController) {         // In the simplest, most efficient, case, reload the table view.         self.tableView.reloadData()     }     */}

 

////  DetailViewController.swift//  Blog Reader////  Created by zcdll on 16/1/24.//  Copyright © 2016年 ZC. All rights reserved.//import UIKitclass DetailViewController: UIViewController {    @IBOutlet weak var webview: UIWebView!    var detailItem: AnyObject? {        didSet {            // Update the view.            self.configureView()        }    }    func configureView() {        // Update the user interface for the detail item.        if let detail = self.detailItem {            if let postWebView = self.webview {                                postWebView.loadHTMLString(detail.valueForKey("content")!.description, baseURL: NSURL(string: "https://googleblog.blogspot.com/"))                            }        }    }    override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.        self.configureView()    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }}

工程:

转载于:https://www.cnblogs.com/zcdll/p/5154965.html

你可能感兴趣的文章
Dubbo的总体架构
查看>>
Spring Cloud微服务架构代码结构详细讲解
查看>>
以太经典硬分叉:矿工欢喜、投资者欢庆、社区高兴的“三赢”之举
查看>>
我的友情链接
查看>>
LVS启(禁)用成员
查看>>
innobackupex 备份报错
查看>>
2016 IT 运维工作计划及学习
查看>>
将一个数的二进制位模式从左到右翻转并输出
查看>>
jQuery学习之jQuery Ajax用法详解
查看>>
关于JEPLUS软件介绍——JEPLUS软件快速开发平台
查看>>
动态增加UIView到当前视图中
查看>>
怎么能看透信封
查看>>
css正方形照片墙
查看>>
找工作的程序员必懂的Linux
查看>>
shell脚本实现杨辉三角形
查看>>
ComponentOne 2019V1火热来袭!全面支持 Visual Studio 2019
查看>>
装了一款系统优化工具,如何从Mac上卸载MacBooster 7?
查看>>
使用符号表调试release程序
查看>>
Delphi 设置系统默认打印机
查看>>
AliOS Things网络适配框架 - SAL
查看>>