swift第四周實訓心得

2016年12月11日 星期日 晚

這周總結的較少,時間也比較緊,希望大家見諒,但總結的都是比較重要的部分,希望有助于大家的學習和復習,下周一定會給大家一個更詳細的總結,么么噠??

day15

微信界面

  • 新建一個JNTViewController.swift繼承自UITabBarController,在其中進行控制器的創建:
override func viewDidLoad() {
        super.viewDidLoad()
        let vc = ViewController()  //創建控制器
        self.addChildVC(vc: vc, title: "消息", image: UIImage(named: "tabbar_mainframe")!, selectImage: UIImage(named: "tabbar_mainframeHL")!.withRenderingMode(.alwaysOriginal))
        
        let contact = ContactViewController()
        self.addChildVC(vc: contact, title: "聯系人", image: UIImage(named: "tabbar_contacts")!, selectImage: UIImage(named: "tabbar_contactsHL")!.withRenderingMode(.alwaysOriginal))
        
        let discover = DiscoverViewController()
         self.addChildVC(vc: discover, title: "發現", image: UIImage(named: "tabbar_discover")!, selectImage: UIImage(named: "tabbar_discoverHL")!.withRenderingMode(.alwaysOriginal))
        
        let I = IViewController()
        self.addChildVC(vc: I, title: "我", image: #imageLiteral(resourceName: "tabbar_me.png"),selectImage: #imageLiteral(resourceName: "tabbar_meHL.png").withRenderingMode(.alwaysOriginal))
        
    }

    //1 控制器
    //2 tabbar文字
    //3 圖片
    //4 選中圖片
    func addChildVC(vc: UIViewController, title: String, image: UIImage, selectImage: UIImage) {
        //設置導航控制器
        let vcN = JNViewController(rootViewController: vc)
        //把導航控制器設置為子控制器給tabbar管理
        self.addChildViewController(vcN)
        //設置文字、圖片
        vcN.tabBarItem = UITabBarItem(title: title, image: image, selectedImage: selectImage)
        let color = UIColor.init(colorLiteralRed: 40.0 / 255.0, green: 177.0 / 255.0, blue: 26.0 / 255.0, alpha: 1.0)
        //修改文字
        let dic = [NSFontAttributeName: UIFont.systemFont(ofSize: 12),NSForegroundColorAttributeName: color]
        vcN.tabBarItem.setTitleTextAttributes(dic, for: .selected)
        
        let dic1 = [NSFontAttributeName: UIFont.systemFont(ofSize: 12), NSForegroundColorAttributeName: UIColor.lightGray]
        vcN.tabBarItem.setTitleTextAttributes(dic1, for: .normal)
        
        self.addChildViewController(vcN)
    }
  • 新建一個JNViewController.swift繼承自UINavigationController,用來修改狀態欄的屬性:
override func viewDidLoad() {
        super.viewDidLoad()
        let color = UIColor.init(colorLiteralRed: 55.0 / 255.0, green: 53 / 255.0,blue: 60 / 255.0, alpha: 1.0)
        self.navigationBar.barTintColor = color;
        //修改狀態欄的顏色
        let dic = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 20),NSForegroundColorAttributeName: UIColor.white]
        self.navigationBar.titleTextAttributes = dic
    }

可以把ViewController設置為微信下邊的第一個,再分別新建微信的下邊另外三個ViewController,分別為:ContactViewController.swift繼承自UIViewController;DiscoverViewController.swift繼承自UIViewController,DiscoverTableViewCell.swift繼承自UITableViewCell;IViewController.swift繼承自UIViewController,ITableViewCell.swift和I2TableViewCell.swift都繼承自UITableViewCell;

  • 在DiscoverViewController.swift中進行“發現”的設置:

    • 先創建一個類
  class CellMode : NSObject {
    var title : String? = nil
    var image : String? = nil
}
  • DiscoverViewController要繼承UITableViewDelegate, UITableViewDataSource兩個協議;

    • 在DiscoverViewController中鍵入:
//放分組對應的數據
  var arr : [[CellMode]] = [[CellMode]]()
  
  var tableView : UITableView? = nil
  override func viewDidLoad() {
      super.viewDidLoad()
      self.title = "發現"
      self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
      self.tableView = UITableView(frame: self.view.bounds, style: .grouped)
      self.tableView?.delegate = self
      self.tableView?.dataSource = self
      self.view.addSubview(self.tableView!)
      self.load()
      
      //注冊
      self.tableView?.register(DiscoverTableViewCell.self, forCellReuseIdentifier: "tag")
      
  }
  
  func load() {
      let mode1 = CellMode()
      mode1.title = "朋友圈"
      mode1.image = "ff_IconShowAlbum"
      let arr = [mode1]
      
      let mode2 = CellMode()
      mode2.title = "掃一掃"
      mode2.image = "ff_IconQRCode"
      
      let mode3 = CellMode()
      mode3.title = "搖一搖"
      mode3.image = "ff_IconShake"
      let arr1 = [mode2, mode3]
      
      let mode4 = CellMode()
      mode4.title = "附近的人"
      mode4.image = "ff_IconLocationService"
      
      let mode5 = CellMode()
      mode5.title = "漂流瓶"
      mode5.image = "ff_IconBottle"
      let arr2 = [mode4, mode5]
      
      
      let mode6 = CellMode()
      mode6.title = "購物"
      mode6.image = "CreditCard_ShoppingBag"
      
      let mode7 = CellMode()
      mode7.title = "游戲"
      mode7.image = "Userguide_Gamecenter_icon"
      let arr3 = [mode6, mode7]
      
      self.arr.append(arr)
      self.arr.append(arr1)
      self.arr.append(arr2)
      self.arr.append(arr3)
      
  }
  
  func numberOfSections(in tableView: UITableView) -> Int {
          return self.arr.count
  }
  
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return self.arr[section].count
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "tag", for: indexPath) as! DiscoverTableViewCell
      //取出模型
      let m = self.arr[indexPath.section][indexPath.row]
      //cell.imageV?.image = UIImage(named: m.image!)
      //cell.titleLabel?.text = m.title
      cell.model = m
      //取消選中按鈕
      cell.selectionStyle = .none
      //詳情按鈕
      cell.accessoryType = .disclosureIndicator
      
      return cell
  }
  
  //調整上頁邊距
  func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
      return 0.1
  }
  
  func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
      return UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
  }
  
  //每個分組之間的間距
  func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
      return 10
  }
  • 在DiscoverTableViewCell中鍵入:
private var imageV : UIImageView? = nil
    private var titleLabel : UILabel? = nil
    var model : CellMode {
        set {
            //newValue傳進來默認值
            self.imageV?.image = UIImage(named: newValue.image!)
            self.titleLabel?.text = newValue.title
        }
        get {
            return CellMode()
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.imageV = UIImageView(frame: CGRect(x: 15, y: 5, width: 30, height: 30))
        self.addSubview(self.imageV!)
        
        self.titleLabel = UILabel(frame: CGRect(x: 55, y: 5, width: 200, height: 30))
        self.addSubview(self.titleLabel!)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
  • 在IViewController中新建一個類:
class CellMode1 : NSObject {
    var image1 : String? = nil
    var title1 : String? = nil
    var title2 : String? = nil
    var image2 : String? = nil
}
  • IViewController要繼承UITableViewDelegate, UITableViewDataSource兩個協議;

  • 在IViewController中鍵入:

//放分組對應數據
    var arr : [[CellMode1]] = [[CellMode1]]()
    var tableView : UITableView? = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        self.title = "我"
        self.tableView = UITableView(frame: self.view.bounds, style: .grouped)
        self.tableView?.delegate = self
        self.tableView?.dataSource = self
        self.view.addSubview(self.tableView!)
        self.load()
        
        //注冊
        self.tableView?.register(I2TableViewCell.self, forCellReuseIdentifier: "tag")
        self.tableView?.register(ITableViewCell.self, forCellReuseIdentifier: "tag2")

    }
    func load() {
        let mode0 = CellMode1()
        mode0.image1 = "nv.jpg"
        mode0.title1 = "大叔的小蘿莉"
        let arr0 = [mode0]
        
        let mode1 = CellMode1()
        mode1.title2 = "相冊"
        mode1.image2 = "MoreMyAlbum"
        let mode2 = CellMode1()
        mode2.title2 = "收藏"
        mode2.image2 = "MoreMyFavorites"
        let mode3 = CellMode1()
        mode3.title2 = "錢包"
        mode3.image2 = "MoreMyBankCard"
        let mode4 = CellMode1()
        mode4.title2 = "卡包"
        mode4.image2 = "MyCardPackageIcon"
        let arr1 = [mode1, mode2, mode3, mode4]
        
        let mode5 = CellMode1()
        mode5.title2 = "表情"
        mode5.image2 = "MoreExpressionShops"
        let arr2 = [mode5]
        
        let mode6 = CellMode1()
        mode6.title2 = "設置"
        mode6.image2 = "MoreSetting"
        let arr3 = [mode6]
        
        self.arr.append(arr0)
        self.arr.append(arr1)
        self.arr.append(arr2)
        self.arr.append(arr3)

    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.arr.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.arr[section].count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
         let cell = tableView.dequeueReusableCell(withIdentifier: "tag", for: indexPath) as! I2TableViewCell
        //取出模型
        let m = self.arr[indexPath.section][indexPath.row]
        //cell.imageH?.image = UIImage(named: m.image1!)
        //cell.nameLabel?.text = m.title1
            cell.mode2 = m
        //取消選中按鈕
        cell.selectionStyle = .none
        //詳情按鈕
        cell.accessoryType = .disclosureIndicator
        return cell
        } else {
           let cell = tableView.dequeueReusableCell(withIdentifier: "tag2", for: indexPath) as! ITableViewCell
            let m = self.arr[indexPath.section][indexPath.row]
            //cell.imageV?.image = UIImage(named: m.image2!)
            //cell.titleLabel?.text = m.title2
            cell.mode1 = m

            //取消選中按鈕
            cell.selectionStyle = .none
            //詳情按鈕
            cell.accessoryType = .disclosureIndicator
            
            return cell

        }
        //cell.model = m
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 100
        } else {
            return 50
        }
    }
    
    //調整上頁邊距
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
    }
    
    //每個分組之間的間距
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
  • 在ITableViewCell中鍵入:
var imageV : UIImageView? = nil
    var titleLabel : UILabel? = nil
    
    var mode1 : CellMode1 {
        set {
            self.imageV?.image = UIImage(named: newValue.image2!)
            self.titleLabel?.text = newValue.title2
        }
        get {
            return CellMode1()
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    
        self.imageV = UIImageView(frame: CGRect(x: 15, y: 5, width: 30, height: 30))
        self.addSubview(self.imageV!)
        
        self.titleLabel = UILabel(frame: CGRect(x: 55, y: 5, width: 200, height: 30))
        self.addSubview(self.titleLabel!)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
  • 在I2TableViewCell中鍵入:
var imageH : UIImageView? = nil
    var nameLabel : UILabel? = nil
    var mode2 : CellMode1 {
        set {
            self.imageH?.image = UIImage(named: newValue.image1!)
            self.nameLabel?.text = newValue.title1
        }
        get {
            return CellMode1()
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.imageH = UIImageView(frame: CGRect(x: 15, y: 5, width: 80, height: 80))
        self.imageH?.layer.cornerRadius = 8
        self.imageH?.layer.masksToBounds = true
        self.addSubview(self.imageH!)
        
        self.nameLabel = UILabel(frame: CGRect(x: 105, y: 5, width: 200, height: 40))
        self.nameLabel?.font = UIFont.systemFont(ofSize: 20)
        self.addSubview(self.nameLabel!)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
  • 在ViewController中對背景顏色進行設置:
override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
    }
  • 運行結果:

day16

新聞頁面

  • 新建三個UIViewCell,分別為:TableViewCell,STableViewCell,TTableViewCell,并分別創建xib文件:

    • 分別在這幾個xib中進行控件的拖動及約束的設置,三個圖如下所示:


  • 把相應的控件按住control拖動到相應的TableViewCell中設置為全局變量;

  • ViewController繼承UITableViewDelegate, UITableViewDataSource協議;

  • 向ViewController中鍵入:

var tabelview : UITableView! = nil
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tabelview = UITableView(frame: self.view.frame, style: .grouped)
        self.tabelview.delegate = self
        self.tabelview.dataSource = self
        self.view.addSubview(self.tabelview)
        //注冊
        self.tabelview.register(UINib(nibName: "TableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "tag")
        self.tabelview.register(UINib(nibName: "STableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "tag1")
        self.tabelview.register(UINib(nibName: "TTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "tag2")
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0{
        let cell = tableView.dequeueReusableCell(withIdentifier: "tag", for: indexPath) as! TableViewCell
        cell.titlelabel.text = "摩爾金融"
        cell.detaillabel.text = "2016年投資展望:大勢分析+10大機會+10大金股"
        cell.imageview.image = UIImage(named: "1")
        cell.selectionStyle = .none
        return cell
        } else if indexPath.row == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "tag1", for: indexPath) as! STableViewCell
            cell.nameLabel.text = "做封面人物 秀出我的態度"
            cell.firstimage.image = UIImage(named: "2")
            cell.secondimage.image = UIImage(named: "3")
            cell.thirdimage.image = UIImage(named: "4")
            cell.selectionStyle = .none
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "tag2", for: indexPath) as! TTableViewCell
            cell.titleL.text = "新聞資訊"
            cell.image1.image = UIImage(named: "5")
            cell.selectionStyle = .none
            return cell
        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 100
        } else if indexPath.row == 1 {
            return 150
        } else {
            return 200
        }
    }
  • 運行結果:

day17

運用Main.storyboard

  • 打開Main.storyboard,將第一個ViewController設置為Navigation Controller,再拖動兩個View Controller,第一個View Controller上拖一個button控件,按住control鍵將button連到第二個View Controller上,選擇show,結果如圖:
  • 新建一個RedViewController繼承自UIViewController:
var name : String! = nil
    override func viewDidLoad() {
        super.viewDidLoad()
        print(name)
    }
  • 在ViewController中鍵入:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        //獲取目標控制器
        let vc = segue.destination;
        //判斷目標控制器是不是你想跳轉的
        if vc is RedViewController {
            let descriptionVc = vc as! RedViewController
            descriptionVc.name = "張三"
        }
    }
  • 運行結果:

單元格

  • 在Main.storyboard中進行控件的拖動及約束的設置,如圖:
  • 新建RootTableViewCell繼承自UITableViewCell,將剛才約束好的控件進行拖動至RootTableViewCell中;

  • 將tableView按住control拖動至ViewController;

  • 在ViewController中鍵入:

override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "RootTableViewCell", for: indexPath) as! RootTableViewCell
        cell.titleLabel.text = "張姍"
        cell.headImage.image = UIImage(named: "nv.jpg")
        return cell
    }
  • 運行結果:

電話本

  • 在Main.storyboard中進行如前面的設置,三個頁面,并進行約束,結果如圖:
  • 新建RootTableViewCell繼承自UITableViewCell,將之前設置好的控件headImage
    ,titleLabel,detailLabel按住control拖入NextViewController中;

  • 新建NextViewController繼承自UIViewController,將之前設置好的控件nameTextField
    ,phoneTextField按住control拖入NextViewController中并鍵入:

var name : String! = nil
    var phone : String! = nil
    
    var sendMsg:((String, String)-> Void)! = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.nameTextField.text = self.name
        self.phoneTextField.text = self.phone
    }

    @IBAction func btnAction(_ sender: UIButton) {
        self.sendMsg(self.nameTextField.text!, self.phoneTextField.text!)
        let _ = self.navigationController?.popViewController(animated: true)
    }
  • 在ViewController中拖入tableView并鍵入:
 override func viewDidLoad() {
        super.viewDidLoad()

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       // if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "RootTableViewCell", for: indexPath) as! RootTableViewCell
        cell.headImage.image = UIImage(named: "nv.jpg")
        cell.titleLabel.text = "姓名"
        cell.detailLabel.text = "電話"
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       let _ = self.RootTableView .indexPath(for: sender as! RootTableViewCell)
    //先控制第二個控制器
        let second = segue.destination
        if second is NextViewController {
            let sec = second as! NextViewController
            sec.name = "張三"
            sec.phone = "3333333333"
            sec.sendMsg = {
                print("name = \($0), phone = \($1)")
            }
        }
    }
    
//    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
//        return 0.1
//    }
  • 運行結果:

練習

  • 在Main.storyboard中進行控件的拖動及約束的設置,如圖:
  • 將設置好的控件按住control拖入ViewController;

  • 在viewcontroller中添加兩個方法:

//點擊屏幕時走
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
       self.textfield.resignFirstResponder()   
}
    
//結束編輯
func textFieldDidEndEditing(_ textField: UITextField) {
       self.label1.text = self.textfield.text
}
  • 運行結果:

day18

好友列表

  • 在ViewController中新建一個枚舉:
//控制分組的開關枚舉
enum Switch {
    case open
    case close
}
  • 在ViewController中新建一個手勢類:
//繼承單擊手勢
class Tap : UITapGestureRecognizer {
    var section : Int? = nil
    
}
  • ViewController繼承自UITableViewDelegate, UITableViewDataSource協議:

  • 在ViewController中鍵入:

    var tableView : UITableView! = nil
    //前面是分組的頭部,后面是分組中的人員
    var dic : [String:[String]] = [String:[String]]()
    //keys的數組
    var arr : [String] = [String]()
    
    var SwitchArr : [Switch] = [.close,.close,.close]
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        dic["我的好友???"] = ["張三","李四","王五","趙六","小白"]
        dic["朋友???"] = ["老李","小六","帥帥","張姍"]
        dic["同學???"] = ["瓊哥","一姐","小哥","師俊南","靖媛","貓咪"]
        
        //初始化一個數組
        for item in dic.keys {
            arr.append(item)
        }
        self.tableView = UITableView(frame: self.view.frame, style: .grouped)
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.view.addSubview(self.tableView)
    }

    //頭部視圖
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    
    //自定義頭部視圖
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 50))
        //單擊手勢
        let tap = Tap()
        tap.section = section
        view.addGestureRecognizer(tap)
        tap.addTarget(self, action: #selector(tapAction(sender:)))
        let label = UILabel(frame: CGRect(x: 300, y: 20, width: 120, height: 40))
        if self.SwitchArr[section] == .close {
        label.text = "▼"
        } else {
            label.text = "▲"
        }
        view.addSubview(label)

        let label1 = UILabel(frame: CGRect(x: 20, y: 20, width: 120, height: 40))
        label1.text = self.arr[section]
        view.addSubview(label1)

        return view
        
    }
    
    //打開手勢方法
    func tapAction(sender: Tap) {
        //先取出來section
        let temp = sender.section  //當前點擊第幾個
        
        //取反
        if self.SwitchArr[temp!] == .close {
            self.SwitchArr[temp!] = .open
        } else {
            self.SwitchArr[temp!] = .close

        }
        //刷新表格
        self.tableView.reloadData()
    }
    
    //尾部視圖
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    //多少分組
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.arr.count
    }
    
    //每組多少個
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        
        let Sswitch = self.SwitchArr[section]
        if Sswitch == .close {
            return 0
        }
        
        //先從數組中取出keys,再根據keys值取出對應的數組
        let array = dic[self.arr[section]]
        return (array?.count)!
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "tag")
        let array = dic[self.arr[indexPath.section]]
        let str = array?[indexPath.row]
        cell.textLabel?.text = str
        return cell
    }
  • 運行結果:

(注:由于這周學的都是向xib中拖動控件而不是打代碼,所以詳細的拖動步驟以及約束的設置都沒有向大家詳細的介紹,約束不會的地方以后會為大家再進行總結。)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,255評論 4 61
  • 姓名:易平香 企業名稱:東莞耀升機電有限公司 組別:AT感謝組/272期努力一組 【日精進打卡第84天】 【知~學...
    shine1yi閱讀 136評論 0 0
  • 若干年前,初到這座江南小城,看什么都很新鮮。有一天出門逛街,看見附近鄉下的老奶奶推著輛三輪車賣糯糯的糕,我...
    煙火青青閱讀 304評論 0 0
  • I 每年的教師節,在我們學校的門房里總有一束玫瑰與百合相雜的鮮花在等待著我,屈指算來,已有十多個年頭了。 記得第一...
    云舒丫丫閱讀 579評論 2 4
  • 我相信一見鐘情的怦然心動,但更看好相濡以沫的不離不棄。不曾努力去追尋過一個女生,或因各種羈絆,或因自己不夠勇敢果斷...
    來自宇宙的小小孩閱讀 208評論 0 0