上一话中实现了两个控制器间的传值,最终效果如图:

这是我们的主页面:

在ViewController中我们主页显示的内容是放到不同的数组中的:

var restaurantNames = ["cg1","cg2","cg3","cg4","cg5","cg6","cg7","cg8","cg9","cg10","cg11"] var restaurantImages = ["128.png","129.png","130.png","131.png","132.png","133.png","134.png","135.png","136.png","137.png","138.png","139.png","140.png"]

今天我们想要把主页面中的信息进行整合,反映到跳转页面中,这就要应用到程序中的Model。通过观察,我们每一行所展示的内容,格式上都是一样的,有图片有标题,现在我们把这个模型单独分离出来。新建一个数据模型,也就是一个cocoa touch class,命名为Rest,代码如下:

import UIKitclass Rest: NSObject { var name: String = "" var image: String = "" var location: String = "" var type: String = "" var isVisit: Bool = false init(name: String,image: String,location: String,type: String,isVisit: Bool){ self.name = name self.image = image self.location = location self.type = type self.isVisit = isVisit }}Rest就是我们信息展示的结构,现在新建一个类DataArray,把初始化的信息放到其中,代码如下:

import UIKitclass DataArray: NSObject { var tempArray = [Rest]() //临时变量 var dataArray:[Rest] { get { return tempArray } } override init(){ tempArray = [ Rest(name: "cg1", image: "128.png", location: "xd1", type: "Cafe", isVisit: false), Rest(name: "cg2", image: "129.png", location: "xd2", type: "Cafe", isVisit: false), Rest(name: "cg3", image: "130.png", location: "xd3", type: "Tea", isVisit: false), Rest(name: "cg4", image: "131.png", location: "xd4", type: "Cafe", isVisit: false), Rest(name: "cg5", image: "132.png", location: "xd5", type: "Tea", isVisit: false), Rest(name: "cg6", image: "133.png", location: "xd6", type: "Cafe", isVisit: false), Rest(name: "cg7", image: "134.png", location: "xd7", type: "Cafe", isVisit: false), Rest(name: "cg8", image: "135.png", location: "xd8", type: "Cafe", isVisit: false), Rest(name: "cg9", image: "136.png", location: "xd9", type: "Cafe", isVisit: false), Rest(name: "cg10", image: "137.png", location: "xd10", type: "Cafe", isVisit: false), Rest(name: "cg11", image: "138.png", location: "xd11", type: "Tea", isVisit: false), ] } }

ViewController中的相关信息就没用了,我们可以删除掉,修改后的ViewController代码如下:

import UIKitclass ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { var restArray = DataArray().dataArray var tableView = UITableView() override func viewDidLoad() { super.viewDidLoad() self.navigationItem.title = "cgGo" tableView = UITableView(frame: CGRectMake(0, 0, 320, 568), style: UITableViewStyle.Plain)//定义一个tableview self.view.addSubview(tableView)//不添加看不到 tableView.dataSource = self tableView.delegate = self //之前是在storyboard中设置的,现在改为手动设置 tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "Cell") // Do any additional setup after loading the view, typically from a nib. } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return restArray.count } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1; } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let identiString = "Cell" //代码复用 var cell = tableView.dequeueReusableCellWithIdentifier(identiString,forIndexPath : indexPath) as? CustomTableViewCell if cell == nil { cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: identiString) as? CustomTableViewCell } var rest = restArray[indexPath.row] var restName = rest.name var restLocation = rest.location var imageName = rest.image var restType = rest.type cell?.initWith(imageName, restName: restName, restLocation: restLocation, restType: restType) if rest.isVisit{ cell?.accessoryType = .Checkmark } else { cell?.accessoryType = .None } return cell! } override func prefersStatusBarHidden() -> Bool { //隐藏上边栏中的电量、信号等信息 return true } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { tableView.deselectRowAtIndexPath(indexPath, animated: true) let callActionHandler = {(action:UIAlertAction!) -> Void in let alertMessage = UIAlertController(title: "Service is unavalable", message: "You can choice another rest" , preferredStyle: UIAlertControllerStyle.Alert) alertMessage.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.Default, handler: nil)) self.presentViewController(alertMessage, animated: true, completion: nil) } let option = UIAlertController(title: nil, message: "What are you goning to do?", preferredStyle: UIAlertControllerStyle.ActionSheet) //callAction let callAction = UIAlertAction(title: "Call"+"12020年12月20日3-(indexPath.row)", style: UIAlertActionStyle.Default, handler: callActionHandler)//自定义的callActionHandler来响应点击的事件 //markAction let markAction = UIAlertAction(title: "I'm here", style: UIAlertActionStyle.Default, handler: { (action:UIAlertAction!) ->Void in let cell = tableView.cellForRowAtIndexPath(indexPath) cell?.accessoryType = UITableViewCellAccessoryType.Checkmark //对号 self.restArray[indexPath.row].isVisit = true }) option.addAction(markAction) //cancelAction let cancelAction = UIAlertAction(title: "ok", style: UIAlertActionStyle.Cancel, handler: nil) // self.presentViewController(option, animated: true, completion: nil) let detail = DetailViewController() self.navigationController?.pushViewController(detail, animated: true) var image = restArray[indexPath.row].image detail.imageName = image option.addAction(cancelAction) option.addAction(callAction) } func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 80 } func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { self.restArray.removeAtIndex(indexPath.row) } self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left) } func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? { let shareAction = UITableViewRowAction(style: .Default, title: "Share", handler: { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in let menu = UIAlertController(title: "Share Action", message: nil, preferredStyle: .ActionSheet) let csdnAction = UIAlertAction(title: "csdn", style: .Default, handler: nil) menu.addAction(csdnAction) let cancelAction = UIAlertAction(title:"Cancel", style: .Cancel, handler: nil) menu.addAction(cancelAction) self.presentViewController(menu, animated: true, completion: nil) }) let deleteAction = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action: UITableViewRowAction!,indexPath: NSIndexPath!) -> Void in })return [deleteAction, shareAction] } }

可以看到之前单个的数组被名为restArray的model取代了,在定义cel的代理方法中引用restArray实例,显示效果如图:

model存储 swift_Swift语言IOS8开发战记10.Data Model相关推荐

  1. ExtJS教程(5)---Ext.data.Model之高级应用

    1.Model的数据验证 这里借助官方的一个例子来说Model数据验证的基本用法 Ext.define('User', {extend: 'Ext.data.Model',fields: [{ nam ...

  2. iOS8开发视频教程Swift语言版-Part 10:iOS的数据持久化-关东升-专题视频课程

    iOS8开发视频教程Swift语言版-Part 10:iOS的数据持久化-17164人已学习 课程介绍         本课程主要介绍了iOS数据持久化的方式,沙箱目录,以及属性列表和对象归档,并且重 ...

  3. iOS8开发视频教程Swift语言版-Part 11:访问Web Service-关东升-专题视频课程

    iOS8开发视频教程Swift语言版-Part 11:访问Web Service-15288人已学习 课程介绍         本课程介绍了,iOS开发中,客户端与服务器端网络通信,介绍了XML和Js ...

  4. iOS8开发视频教程Swift语言版-Part 9:iOS分层架构设-关东升-专题视频课程

    iOS8开发视频教程Swift语言版-Part 9:iOS分层架构设-8532人已学习 课程介绍         传统企业级项目开发需要低耦合性,那么iOS这种相对比较小的应用开发需要么?答案是肯定的 ...

  5. 编程之法-C语言应用开发与工程实践-C语言概述

    浅谈计算机系统架构 计算机硬件系统 现代计算机是由运算器.控制器.存储器.输入设备.输出设备五大部分组成,它们各司其职,完成了数据的计算.存储.传输任务,整体架构如下图所示 下面是它们各个组件的功能介 ...

  6. php 微信 语音,PHP语言微信开发:微信录音临时转永久存储

    本文主要向大家介绍了PHP语言微信开发:微信录音临时转永久存储,通过具体的内容向大家展示,希望对大家学习php语言有所帮助. 最近做开发的时候碰到了这个问题,甲方希望用户在微信端的录音能够一直有效.就 ...

  7. Go 语言快速开发入门(基础语法详解,第一节)

    Go 语言快速开发入门(基础语法详解,第一节) 一.基础语法详解,第一节 1.HelloWorld 1.1.代码编写和解释 1.2.go语言注意事项 2.Go 语言的转义字符&&Go ...

  8. c语言编程从入门到精通+pdf下载,编程大讲坛 C语言核心开发技术从入门到精通 陈强 pdf扫描版...

    编程大讲坛 C语言核心开发技术从入门到精通由浅入深地讲解了c语言开发技术,以遵循科学合理的学习计划为主线,通过典型实例来讲解其具体使用.全书内容分为20课,其中第1-8课依次讲解了c语言概述.c语言算 ...

  9. 深入体验c语言项目开发 光盘,深入体验C语言项目开发

    c语言是当今使用最为频繁的编程语言之一,一直在开发领域占据重要的地位.<深入体验C语言项目开发>通过10个综合实例的实现过程,详细讲解了c语言在实践项目中的综合运用过程.这些项目从作者的学 ...

最新文章

  1. 员工提出离职,称害怕猝死,HR却说:先猝死了再说!
  2. 计算的极限(零):逻辑与图灵机
  3. firefox浏览器 插件--【维基百科+谷歌翻译】高级应用之 带图翻译
  4. pyecharts开篇(python可视化神器)
  5. 简述一下动态sql的执行原理_简述一下Pogo Pin电连接器的设计原理和考虑因素
  6. 【校招面试 之 网络】第3题 HTTP请求行、请求头、请求体详解
  7. 用反射简化 asp.net 报表的一点总结
  8. Java——面向接口编程
  9. 【2019.09.14】2019icpc沈阳网络赛
  10. Opera Mobile Classic Emulator
  11. 如何在 Swift 中进行错误处理
  12. (一)Quartz2.2.1 简单例子
  13. 教你Java 代码性能优化小妙招,速速来看
  14. neo4j社区版下载
  15. 软考 软件设计师考试经验分享、题型分析
  16. 思岚雷达rplidar S1配置调试全纪录
  17. MK802与外部硬件设备的通讯
  18. SDI Over IP相关标准及技术简介
  19. 指数随机变量 泊松过程跳_泊松过程
  20. 部分彩色图片处理方式的C++实现

热门文章

  1. 两种方法动态获得ABAP类的class attribute的值
  2. Cloud for Customer UI里点了超链接后的跳转处理
  3. 打开My Lead detail page会发生timeout的错误的解决方案
  4. 第三方应用如何在SAP Kyma上进行服务注册
  5. ABAP 并发执行的威力
  6. 打印基于某category创建的所有product
  7. Field EXP_REVENUE
  8. TIMEOUT will also publish one order event
  9. SAP ABAP DDICSAP ABAP DDIC table runtime object table runtime object
  10. 推荐一个采用方便程序员在线动画学习常用算法的良心网站