暑期项目开发实训 Day9

今天按理说应该完成第三个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
    }()



   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

国产酱香科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值