最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS開發(fā)之表視圖詳解

 更新時間:2016年11月01日 10:09:49   作者:MichaelLau  
表視圖是iOS開發(fā)中最重要的視圖,它以列表的形式展示數(shù)據(jù)。本篇文章詳細的介紹了表視圖的幾種用法,有需要的小伙伴可以了解一下。

本文詳細介紹了表視圖的用法。具體如下:

概述

表視圖組成

表視圖是iOS開發(fā)中最重要的視圖,它以列表的形式展示數(shù)據(jù)。表視圖又一下部分組成:

  • 表頭視圖:表視圖最上邊的視圖

  • 表腳視圖:表視圖最下邊的視圖

  • 單元格(cell):表視圖中每一行的視圖

  • 節(jié)(section):由多個單元格組成,應(yīng)用于分組列表

    • 節(jié)頭

    • 節(jié)腳

表視圖的相關(guān)類

UITableView繼承自UIScrollView,且有兩個協(xié)議:UITableViewDelegate和UITableViewDataSource。此外UITableViewCell類時單元格類,UITableViewController類時UITableView的控制器,UITableViewHeaderFooterView用于為節(jié)頭和節(jié)腳提供視圖。

表視圖分類

  • 普通表視圖:主要用于動態(tài)表,而動態(tài)表一般在單元格數(shù)目未知的情況下使用
  • 分組表視圖:一般用于靜態(tài)表,用來進行界面布局

單元格的組成和樣式

單元格由圖標、主標題、副標題、擴展視圖組成,可以根據(jù)需要進行選擇,其中內(nèi)置的擴展視圖在枚舉類型

Swift枚舉成員 Objective-C枚舉成員 說明
none ITableViewCellAccessoryNone 沒有擴展圖標
disclosureIndicator UITableViewCellAccessoryDisclosureIndicator 擴展指示器,為箭頭+問號
detailDisclosureButton UITableViewCellAccessoryDetailDisclosureButton 細節(jié)展示圖,為問號
checkmark UITableViewCellAccessoryCheckmark 選中標志,圖標為勾
detailButton UITableViewCellAccessoryDetailButton 細節(jié)詳情展示,圖標為問號

內(nèi)置的單元格樣式在枚舉類型UITableViewCellStyle中定義:

Swift枚舉成員 Objective-C枚舉成員 說明
default UITableViewCellStyleDefault 默認樣式
subtitle UITableViewCellStyleSubtitle 有圖標、主標題、副標題、副標題在主標題的下面
value1 UITableViewCellStyleValue1 有主標題、副標題,主標題左對齊、副標題右對齊,可以有圖標
2alue3 UITableViewCellStyleValue2 有主標題、副標題,主標題和副標題居中對齊,無圖標

數(shù)據(jù)源協(xié)議與委托協(xié)議

UITableViewDataSource

數(shù)據(jù)源協(xié)議主要為表視圖提供數(shù)據(jù),主要方法如下

方法 返回類型 說明
func tableView(UITableView, cellForRowAt: IndexPath) UITableViewCell 為表視圖單元格提供數(shù)據(jù),必須實現(xiàn)
tableView(UITableView, numberOfRowsInSection: Int) Int 返回某個節(jié)中的行數(shù),必須實現(xiàn)
tableView(UITableView, titleForHeaderInSection: Int) String 返回節(jié)頭的標題
tableView(UITableView, titleForFooterInSection: Int) String 返回節(jié)腳的標題
numberOfSections(in: UITableView) Int 返回節(jié)的個數(shù)
sectionIndexTitles(for: UITableView) [String]? 返回表示圖節(jié)索引標題

UITableViewDelegate

委托協(xié)議主要主要用來設(shè)定表視圖中節(jié)頭和節(jié)腳的標題,以及一些動作事件,主要方法如下

方法 返回類型 說明
tableView(UITableView, didSelectRowAt: IndexPath) 單元格響應(yīng)事件
tableView(UITableView, accessoryButtonTappedForRowWith: IndexPath) 擴展視圖響應(yīng)事件

簡單表視圖

UIViewController根視圖控制器實現(xiàn)表視圖

步驟

  1. 創(chuàng)建一個iOS工程
  2. 從對象庫中拖入一個TableView到storyboard文件中,并將TableView覆蓋整個View
  3. 打開Table View的屬性檢查器,將PrototypeCells的值設(shè)為1,注意不要添加多個,否則會發(fā)生錯誤;此時Table View會添加一個Table View Cell。
  4. 打開Table View Cell的屬性檢查器,設(shè)置Identifier屬性。
  5. 注冊UITableViewDataSource和UITableViewDelegate協(xié)議
  6. 編寫代碼實現(xiàn)功能

實現(xiàn)

//
// ViewController.swift
// TableViewDemo
//
// Created by Michael on 2016/10/26.
// Copyright © 2016年 Michael. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
 
 //全部數(shù)據(jù)
 var listItems: NSArray!

 override func viewDidLoad() {
  super.viewDidLoad()
  
  //讀取資源文件數(shù)據(jù)
  let listPath = Bundle.main.path(forResource: "team", ofType: "plist")
  self.listItems = NSArray(contentsOfFile: listPath!)
 }

 override func didReceiveMemoryWarning() {
  super.didReceiveMemoryWarning()
  // Dispose of any resources that can be recreated.
 }

 //返回列表每行的視圖
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 //根據(jù)Identifier找到Cell
  let cell = tableView.dequeueReusableCell(withIdentifier: "CustomId", for: indexPath)
  let row = indexPath.row
  
  let rowDict = self.listItems[row] as! NSDictionary
  cell.textLabel?.text = rowDict["name"] as? String
  cell.detailTextLabel?.text = "123"
  
  let imagePath = String(format: "%@.png", rowDict["image"] as! String)
  cell.imageView?.image = UIImage(named: imagePath)
  cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
  return cell
 }

 //返回條目數(shù)目
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return self.listItems.count
 }
 
 //響應(yīng)條目點擊事件
 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  print("點擊事件")
 }
 
}

示例圖

none模式

disclosureIndicator

UITableViewController根視圖控制器實現(xiàn)表視圖
步驟

  1. 創(chuàng)建一個iOS工程
  2. 刪除storyboard中View Controller Scene 中的View Controller,再從對象庫拖入一個Table View Controller到設(shè)計界面
  3. 打開Table View Controller屬性檢查器,勾選Is Initial View Controller選項,否則應(yīng)用啟動后是黑屏
  4. 將ViewController類的父類由UIViewController改為UITableViewController
  5. 打開View Controller的屬性選擇器在Class列表中選擇ViewController
  6. UITableViewController默認以注冊UITableViewDataSource和UITableViewDelegate協(xié)議,不需要再注冊

實現(xiàn)

import UIKit

class ViewController: UITableViewController {
 
 //全部數(shù)據(jù)
 var listItems: NSArray!

 override func viewDidLoad() {
  super.viewDidLoad()
  
  //讀取資源文件數(shù)據(jù)
  let listPath = Bundle.main.path(forResource: "team", ofType: "plist")
  self.listItems = NSArray(contentsOfFile: listPath!)
 }

 override func didReceiveMemoryWarning() {
  super.didReceiveMemoryWarning()
  // Dispose of any resources that can be recreated.
 }

 //返回列表每行的視圖
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCell(withIdentifier: "CustomId", for: indexPath)
  let row = indexPath.row
  
  let rowDict = self.listItems[row] as! NSDictionary
  cell.textLabel?.text = rowDict["name"] as? String
  cell.detailTextLabel?.text = "123"
  
  let imagePath = String(format: "%@.png", rowDict["image"] as! String)
  cell.imageView?.image = UIImage(named: imagePath)
  cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
  return cell
 }

 //返回條目數(shù)目
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return self.listItems.count
 }
 
 //響應(yīng)條目點擊事件
 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  print("點擊事件")
 }
 
}

示例圖

detailButton模式

checkmark模式

自定義單元格

步驟

  1. 創(chuàng)建一個表視圖工程
  2. 修改根視圖控制器為表視圖控制器UITableViewController,參照上節(jié)的步驟
  3. 從對象庫中拖入控件到單元格內(nèi)部,比如Lable和ImageView
  4. 創(chuàng)建自定義單元格類CustomCell文件,并繼承UITableViewCell類
  5. 在設(shè)計界面中選擇View Controller Scene中的Table View Cell,并打開屬性檢查器,將Class設(shè)為CustomCell類,并設(shè)置單元格的Identifier
  6. 為單元格中的控件Label和ImageView控件連接輸出接口,將控件綁定到CustomCell類中
  7. 打開ViewController類,編寫代碼實現(xiàn)

實現(xiàn)
CustomCell類

//
// CustomCell.swift
// CustomCell
//
// Created by Michael on 2016/10/25.
// Copyright © 2016年 Michael. All rights reserved.
//

import UIKit

class CustomCell: UITableViewCell {

 @IBOutlet weak var mImage: UIImageView!
 @IBOutlet weak var mLabel: UILabel!
 override func awakeFromNib() {
  super.awakeFromNib()
  // Initialization code
 }

 override func setSelected(_ selected: Bool, animated: Bool) {
  super.setSelected(selected, animated: animated)

  // Configure the view for the selected state
 }

}

ViewController類

//
// ViewController.swift
// SimpleTableView
//
// Created by Michael on 2016/10/24.
// Copyright © 2016年 Michael. All rights reserved.
//

import UIKit

class ViewController: UITableViewController {
 
 var listItems: NSArray!
 
 override func viewDidLoad() {
  super.viewDidLoad()
  // Do any additional setup after loading the view, typically from a nib.
  let listPath = Bundle.main.path(forResource: "team", ofType: "plist")
  self.listItems = NSArray(contentsOfFile: listPath!)
 }
 
 override func didReceiveMemoryWarning() {
  super.didReceiveMemoryWarning()
  // Dispose of any resources that can be recreated.
 }
 
 
 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return self.listItems.count
 }
 
 
 
 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 //找到自定義單元格
  let cell:CustomCell! = tableView.dequeueReusableCell(withIdentifier: "CustomCellId", for: indexPath) as? CustomCell
  //let cell = UITableViewCell(style: .value1, reuseIdentifier: "CellIdentifier")
  let row = indexPath.row
  
  let rowDict = self.listItems[row] as! NSDictionary
  //設(shè)置控件屬性
  cell.mLabel.text = rowDict["name"] as? String
  
  let imagePath = String(format: "%@.png", rowDict["image"] as! String)
  cell.mImage.image = UIImage(named: imagePath)
  cell.accessoryType = .disclosureIndicator
  return cell
  
 }
}

示例圖

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS開發(fā)之如何獲取視圖在屏幕中顯示的位置

    iOS開發(fā)之如何獲取視圖在屏幕中顯示的位置

    這篇文章主要給大家介紹了關(guān)于iOS開發(fā)之如何獲取視圖在屏幕上顯示的位置,以及ios 獲取控件相對屏幕的位置的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • iOS tableView實現(xiàn)單選和多選的實例代碼

    iOS tableView實現(xiàn)單選和多選的實例代碼

    本篇文章主要介紹了iOS tableView實現(xiàn)單選和多選的實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • Android中g(shù)etActivity()為null的解決辦法

    Android中g(shù)etActivity()為null的解決辦法

    在Android開發(fā)的時候可能遇過出現(xiàn)getActivity()出現(xiàn)null的時候?qū)е鲁绦驁蟪隹罩羔槷惓#敲从龅竭@種情況改如何解決,下面跟著小編一起去看看。
    2016-08-08
  • IOS代碼筆記UIView的placeholder的效果

    IOS代碼筆記UIView的placeholder的效果

    這篇文章主要為大家詳細介紹了IOS實現(xiàn)placeholder效果的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • 詳解iOS11關(guān)于導(dǎo)航欄問題

    詳解iOS11關(guān)于導(dǎo)航欄問題

    本篇文章主要介紹了詳解iOS11關(guān)于導(dǎo)航欄問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • iOS圖片壓縮、濾鏡、剪切及渲染等詳解

    iOS圖片壓縮、濾鏡、剪切及渲染等詳解

    這篇文章主要給大家介紹了關(guān)于iOS圖片壓縮、濾鏡、剪切及渲染等的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • iOS中textField限制字符串長度、字符數(shù)的方法

    iOS中textField限制字符串長度、字符數(shù)的方法

    這篇文章主要給大家介紹了關(guān)于iOS中textField限制字符串長度、字符數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • iOS 防止按鈕多次點擊造成多次響應(yīng)的方法

    iOS 防止按鈕多次點擊造成多次響應(yīng)的方法

    這篇文章主要介紹了iOS 防止按鈕多次點擊造成多次響應(yīng)的方法的相關(guān)資料,這里對多次點擊造成的響應(yīng)提供了解決辦法,需要的朋友可以參考下
    2016-11-11
  • 實例分析IOS實現(xiàn)自動打包

    實例分析IOS實現(xiàn)自動打包

    本篇文章給大家分享了IOS實現(xiàn)自動打包的相關(guān)知識點,以及需要的操作內(nèi)容做了分享,有需要的朋友可以學(xué)習(xí)下。
    2018-05-05
  • 使用Xcode為iOS應(yīng)用項目創(chuàng)建PCH文件的方法及應(yīng)用示例

    使用Xcode為iOS應(yīng)用項目創(chuàng)建PCH文件的方法及應(yīng)用示例

    這篇文章主要介紹了使用Xcode為iOS應(yīng)用項目創(chuàng)建PCH文件的方法及應(yīng)用示例,PCH文件可以保留應(yīng)用的很多的基礎(chǔ)設(shè)置信息以供復(fù)用,需要的朋友可以參考下
    2016-03-03

最新評論

木里| 梓潼县| 杭州市| 永安市| 大兴区| 枣强县| 苗栗市| 黄冈市| 武城县| 安西县| 古蔺县| 碌曲县| 金湖县| 固原市| 兰溪市| 柳州市| 惠州市| 依兰县| 宿迁市| 建德市| 页游| 玉田县| 玉屏| 巴南区| 无为县| 乌鲁木齐县| 辽源市| 武威市| 抚州市| 鄯善县| 遂川县| 隆化县| 洛南县| 甘南县| 金门县| 马关县| 兰坪| 九台市| 东平县| 汉川市| 乌什县|