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

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

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

这是我们的主页面:

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

在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实例,显示效果如图:

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

上一篇:我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。


下一篇:云效助力新零售运维效能提升之路