• iOS开发 Swift开发数独游戏(三) 选关界面


    一、选关界面涉及到的功能点

    1)需要UITableView以及相应数据代理、协议的实现

    2)读取plist文件并转化成模型

    3)在单元格点击后进入数独游戏,涉及到把数据经segue在UIViewController间传递

    4)记录某一关卡是否通过需要用到NSUserDefaults的存档记录功能

    先放一张效果图

     Screen Shot 2015-03-01 at 8.28.47 AM

    二、具体步骤

    1、在

    Storyboard中拖入一个TableViewController,勾选”Is Initial View Controller”,如图

    Screen Shot 2015-03-01 at 8.31.57 AM

    为了方便设计,我还将Size 设为“freeform” 长宽设为1024*768

    Screen Shot 2015-03-01 at 8.34.23 AM

    2、新建一个Swift类ChooseGameTableView,令其继承UITableViewController,并实现UITableViewDataSource协议。在之前的ViewController属性列表中Custom Class输入新建的类。

    Screen Shot 2015-03-02 at 8.36.06 PM

    3、在Swift类ChooseGameTableView中设置UITableView以及相应数据代理、协议的实现 ,代码如下

       1:  //
       2:  //  ChooseGameTableView.swift
       3:  //  sudoku-v02
       4:  //
       5:  //  Created by 张泽阳 on 2/17/15.
       6:  //  Copyright (c) 2015 张泽阳. All rights reserved.
       7:  //
       8:   
       9:  import Foundation
      10:  import UIKit
      11:  class ChooseGameTableView: UITableViewController,UITableViewDataSource {
      12:      // MARK: - 变量
      13:      //swift貌似现在还不支持#pragma mark 这样的写法
      14:      // 写法参考来源:http://www.tuicool.com/articles/JVZjmm
      15:      var passes = [pass]()
      16:      var touchingP:pass = pass(mytitle: "", isPassed: false, prob: "", solves: [""])
      17:      var touchingCellIndex:NSIndexPath?
      18:      // MARK: - View载入时执行方法
      19:      override func viewDidLoad() {
      20:          //读取plist文件
      21:          var path = NSBundle.mainBundle().pathForResource("sudukuR", ofType: "plist")
      22:          var rootDic = NSDictionary(contentsOfFile: path!)
      23:          var PassNum = 0
      24:          var NotFound = "NotFound"//未找到时的标记符
      25:          for i in 1...81 {
      26:              var tempPasses: AnyObject? = rootDic?.objectForKey("D\(i)")//按挖孔数区分难度进行遍历
      27:              if tempPasses == nil {//如果该挖空数下没有关卡则跳过
      28:                  continue
      29:              }
      30:              var CurrentDPasses  = tempPasses as NSArray //转成NSArray
      31:              for SinglePassDic in CurrentDPasses {//遍历得到每一关的单独关卡(Dictionary格式)
      32:                  //数据转模型,添加进passes中
      33:                  passes.append(pass(mytitle: "第\(++PassNum)关 挖空:\(i)", isPassed: getIsPassedByMD5(SinglePassDic.objectForKey("p") as String), prob: SinglePassDic.objectForKey("p") as String, solves: SinglePassDic.objectForKey("s") as [String]))
      34:              }
      35:          }
      36:          self.tableView.dataSource = self
      37:       }
      38:     
      39:      // MARK: - 检查该关卡之前是否已经通过
      40:      func getIsPassedByMD5(pro:String)->Bool{
      41:          var md5 = (pro as NSString).md5()
      42:          //md5加密参考来源:http://minishine.sinaapp.com/?p=86
      43:          //        NSUserDefaults.standardUserDefaults().
      44:          //        println(NSUserDefaults.standardUserDefaults().boolForKey(md5))
      45:          return NSUserDefaults.standardUserDefaults().boolForKey(md5)
      46:      }
      47:     
      48:      // MARK: - tableview一系列设置
      49:      override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
      50:          return 1
      51:      }
      52:      override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      53:          return passes.count
      54:      }
      55:      override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      56:          let cell = tableView.dequeueReusableCellWithIdentifier("pass") as UITableViewCell
      57:          let myPassCell = passes[indexPath.row]
      58:  //        var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
      59:          cell.textLabel?.text = myPassCell.title
      60:          cell.tag = indexPath.row
      61:          if myPassCell.passed {
      62:               cell.accessoryType = UITableViewCellAccessoryType.Checkmark
      63:              cell.backgroundColor = UIColor.greenColor()
      64:          }else{
      65:              cell.accessoryType = UITableViewCellAccessoryType.None
      66:              cell.backgroundColor = UIColor.whiteColor()
      67:          }
      68:         
      69:          
      70:          return cell
      71:      }
      72:      //    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
      73:      //        return "title"
      74:      //    }
      75:      override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
      76:          println("touch")
      77:          //        touchingP = passes[indexPath.row]
      78:          touchingCellIndex = indexPath
      79:          //         println("delivered")
      80:      }
      81:      
      82:      // MARK: - 设置通知监听机制
      83:      override func viewWillAppear(animated: Bool) {
      84:          
      85:          NSNotificationCenter.defaultCenter().addObserver(self, selector:"reloadData2", name: "reload", object: nil)
      86:      }
      87:      func reloadData2(){
      88:          self.tableView.reloadData()
      89:          var tCell:UITableViewCell = self.tableView.cellForRowAtIndexPath(touchingCellIndex!)!
      90:          tCell.accessoryType = UITableViewCellAccessoryType.Checkmark
      91:          tCell.backgroundColor = UIColor.greenColor()
      92:      }
      93:   
      94:      // MARK: - 设置segue跳转时传递的参数
      95:      override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!){
      96:  //        println(segue.identifier)
      97:  //        println("sugue")
      98:  //        println((sender as UITableViewCell).tag)
      99:  //        if(segue.identifier == "ShowDetails"){
     100:              var detailViewController:ViewController! = segue.destinationViewController as ViewController
     101:  //            var index = self.tableView.indexPathForSelectedRow()
     102:              detailViewController.prePass = passes[(sender as UITableViewCell).tag]
     103:  //            detailViewController.cellTemp = (sender as UITableViewCell)
     104:  //        }
     105:      }
     106:  }
  • 相关阅读:
    从开发人员角度对软件测试的些许理解
    ObjectiveC的语法
    HttpModule与HttpHandler使用
    我为什么学习HASKELL?
    Linux下C语言编程环境Make命令和Makefile
    一个简单的验证框架
    程序员之路
    ObjectiveC语法之ObjectiveC语言和IOS系统(简介,语法,系统结构)
    Teamcity
    Python进阶 错误处理
  • 原文地址:https://www.cnblogs.com/zeyang/p/4309824.html
Copyright © 2020-2023  润新知