Swift语言IOS8开发战记10.Data Model

简介: 上一话中实现了两个控制器间的传值,最终效果如图:这是我们的主页面:在ViewController中我们主页显示的内容是放到不同的数组中的:var restaurantNames ...

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


这是我们的主页面:


在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 UIKit

class 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 UIKit

class 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 UIKit

class 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"+"180-123-\(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实例,显示效果如图:


目录
相关文章
|
机器学习/深度学习 TensorFlow Swift
Swift语言适合多个领域的开发
Swift语言适合多个领域的开发
608 9
|
安全 编译器 Swift
Swift开发
Swift开发
546 9
|
安全 数据处理 Swift
深入探索iOS开发中的Swift语言特性
本文旨在为开发者提供对Swift语言在iOS平台开发的深度理解,涵盖从基础语法到高级特性的全面分析。通过具体案例和代码示例,揭示Swift如何简化编程过程、提高代码效率,并促进iOS应用的创新。文章不仅适合初学者作为入门指南,也适合有经验的开发者深化对Swift语言的认识。
600 9
|
安全 开发工具 Swift
Swift 是苹果公司开发的现代编程语言,具备高效、安全、简洁的特点,支持类型推断、闭包、泛型等特性,广泛应用于苹果各平台及服务器端开发
Swift 是苹果公司开发的现代编程语言,具备高效、安全、简洁的特点,支持类型推断、闭包、泛型等特性,广泛应用于苹果各平台及服务器端开发。基础语法涵盖变量、常量、数据类型、运算符、控制流等,高级特性包括函数、闭包、类、结构体、协议和泛型。
884 2
|
安全 Swift iOS开发
Swift 与 UIKit 在 iOS 应用界面开发中的关键技术和实践方法
本文深入探讨了 Swift 与 UIKit 在 iOS 应用界面开发中的关键技术和实践方法。Swift 以其简洁、高效和类型安全的特点,结合 UIKit 丰富的组件和功能,为开发者提供了强大的工具。文章从 Swift 的语法优势、类型安全、编程模型以及与 UIKit 的集成,到 UIKit 的主要组件和功能,再到构建界面的实践技巧和实际案例分析,全面介绍了如何利用这些技术创建高质量的用户界面。
677 2
|
Swift iOS开发 UED
如何使用Swift和UIKit在iOS应用中实现自定义按钮动画
【10月更文挑战第18天】本文通过一个具体案例,介绍如何使用Swift和UIKit在iOS应用中实现自定义按钮动画。当用户按下按钮时,按钮将从圆形变为椭圆形并从蓝色渐变为绿色;释放按钮时,动画恢复原状。通过UIView的动画方法和弹簧动画效果,实现平滑自然的动画过渡。
463 5
|
Swift iOS开发 UED
如何使用Swift和UIKit在iOS应用中实现自定义按钮动画
本文通过一个具体案例,介绍如何使用Swift和UIKit在iOS应用中实现自定义按钮动画。当用户点击按钮时,按钮将从圆形变为椭圆形,颜色从蓝色渐变到绿色;释放按钮时,动画以相反方式恢复。通过UIView的动画方法和弹簧动画效果,实现平滑自然的过渡。
507 1
|
安全 API Swift
探索iOS开发中的Swift语言之美
【10月更文挑战第23天】在数字时代的浪潮中,iOS开发如同一艘航船,而Swift语言则是推动这艘船前进的风帆。本文将带你领略Swift的独特魅力,从语法到设计哲学,再到实际应用案例,我们将一步步深入这个现代编程语言的世界。你将发现,Swift不仅仅是一种编程语言,它是苹果生态系统中的一个创新工具,它让iOS开发变得更加高效、安全和有趣。让我们一起启航,探索Swift的奥秘,感受编程的乐趣。
|
安全 Swift iOS开发
探索iOS开发中的Swift语言之美
在数字时代的浪潮中,移动应用已成为日常生活的延伸。本文将深入探讨iOS平台上的Swift编程语言,揭示其背后的设计哲学、语法特性以及如何利用Swift进行高效开发。我们将通过实际代码示例,展示Swift语言的强大功能和优雅简洁的编程风格,引导读者理解并运用Swift解决实际问题。
|
安全 Swift iOS开发
探索iOS开发之旅:Swift语言的魅力与挑战
【9月更文挑战第21天】在这篇文章中,我们将一起潜入iOS开发的海洋,探索Swift这门现代编程语言的独特之处。从简洁的语法到强大的功能,Swift旨在让开发者能够以更高效、更安全的方式构建应用程序。通过实际代码示例,我们会深入了解Swift如何简化复杂任务,并讨论它面临的挑战和未来的发展方向。无论你是初学者还是有经验的开发者,这篇文章都将为你提供新的视角和知识。
412 5

相关课程

更多