暑期项目开发实训 Day9

来源:互联网 发布:怎么抢购淘宝一元拍 编辑:程序博客网 时间:2024/05/18 18:53

今天按理说应该完成第三个demo的,不过由于下午投了一份简历,晚上练了一下背包问题,所以进度搁置了。

目前进度落后了80页。


02 部分: Tagging Locations

这一部分我们想做一个动画效果:

当添加Location描述后,点击Done,屏幕会出现一个透明框提示,经过0.6秒的延迟回到开始页。

具体如下:


书上说:TheCategoryPickerViewController currently does not have a way to communicate backto the LocationDetailsViewController that the user picked a new category.

显然,这里是因为缺少了delegate protocol

但是IOS学徒的作者介绍了工作量更少的一种方法:unwind segues。

Unwindsegues are pretty cool and often easier than using a delegate protocol,

Especiallyfor simple picker screens such as this one.

HUD( Heads-Up Display)

    // prepare for sender (unwind segue)    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {        if segue.identifier == "PickCategory" {            let controller = segue.destination as! CategoryPickerViewController            controller.selectedCategoryName = categoryName        }    }


03 部分:Core Data

这一部分首先介绍了Swift的值类型和引用类型。

常量——值类型,不可改变。引用类型,可以改变。

引用类型本身为常量时,不能放入一个对象中。

值类型是浅拷贝。(对象相等,你改我不改)

引用类型是深拷贝。(对象同一,你改我也改)

记住:类创建的对象是引用类型,其他是值类型。

然后就是下载Liya.  图形化界面 SQLite。链接数据库。

以达到数据持久化, 保存Location的描述。


04 部分:Locations Tab

我们为了操作tableView中的cell,可以加tag。

但是为什么不考虑把cell也变成对象呢?

以下是LocationCell:

    // 不用 viewWithTag    func configure(for location: Location) {        if location.locationDescription.isEmpty {            descriptionLabel.text = "(No Description)"        } else {            descriptionLabel.text = location.locationDescription        }                if let placemark = location.placemark {        var text = ""        if let s = placemark.subThoroughfare {            text += s + " "        }        if let s = placemark.thoroughfare {            text += s + ","        }        if let s = placemark.locality {            text += s        }        addressLabel.text = text        } else {            addressLabel.text = String(format: "Lat: %.8f,Long: %.8f",location.latitude,location.longitude)        }    }


最后记录一下慢启动的问题:(lazy变量)

//    var locations = [Location]()    //replace with:    // lazily loading obj. until you first use them.    // this makes your apps quicker to start and it saves memory.    lazy var fetchedResultsController:                                    NSFetchedResultsController<Location> = {        let fetchRequest = NSFetchRequest<Location>()                let entity = Location.entity()        fetchRequest.entity = entity                                                        let sortDescriptor1 = NSSortDescriptor(key: "category", ascending: true)        let sortDescriptor2 = NSSortDescriptor(key: "date", ascending: true)                                     fetchRequest.sortDescriptors = [sortDescriptor1,sortDescriptor2]                fetchRequest.fetchBatchSize = 20                                                let fetchedResultsController = NSFetchedResultsController(            fetchRequest: fetchRequest,            managedObjectContext: self.managedObjectContext,            sectionNameKeyPath: "category",    ///change            cacheName: "Locations")                                                fetchedResultsController.delegate = self        return fetchedResultsController    }()