UITableView是app开发中经常使用到的控件,功能非常强大,多用于数据的显示。

以下以一个简单的实例来介绍其基本使用方法。



先建一个project
代码例如以下:


import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
private var dataSource: Dictionary<String, [String]>? //定义表格的数据源
private var keyArray: [String]?
private let cellIdef = "zcell" override func viewDidLoad() {
super.viewDidLoad() //初始化数据
demoData() var frame = self.view.bounds
frame.origin.y += 20
frame.size.height -= 20 //初始化表格
var tableView = UITableView(frame: frame, style: UITableViewStyle.Plain)
//设置重用标志
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellIdef)
tableView.tableFooterView = UIView()
tableView.dataSource = self
tableView.delegate = self
self.view.addSubview(tableView) } override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning() } private func demoData() {
dataSource = ["国家": ["中国", "美国", "法国", "德国", "意大利", "英国", "俄罗斯"],
"种族": ["白种人", "黄种人", "黑种人"]
]
keyArray = ["国家", "种族"]
} // MARK: - UITableViewDataSource //设置表格的组数
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return keyArray!.count
} //设置表格每组的行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var array = dataSource![keyArray![section]]
return array!.count
} //设置表格的内容
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdef, forIndexPath: indexPath) as UITableViewCell
var array = dataSource![keyArray![indexPath.section]]
cell.textLabel.text = array![indexPath.row]
return cell } //设置每组的标题
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return keyArray![section]
} //MARK: - UITableViewDelegate func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}

swift的UITableView的使用的更多相关文章

  1. iOS开发——实战篇Swift篇&UItableView结合网络请求,多线程,数据解析,MVC实战

    UItableView结合网络请求,多线程,数据解析,MVC实战 学了这么久的swift都没有做过什么东西,今天就以自己的一个小小的联系,讲一下,怎么使用swift在实战中应用MVC,并且结合后面的高 ...

  2. Swift中UITableView的简单使用

    Swift中的注释 使用"// MARK:- 注释内容",对属性或方法进行注释 使用"///注释内容"对属性或方法提供调用说明的注释 使用extension对同 ...

  3. Swift开发UITableView常用的一些细节知识点介绍

    <code class="objectivec"><strong><span style="font-size:18px;"> ...

  4. Swift 给UITableView 写extension 时 报错 does not conform to protocol 'UITableViewDataSource'

    那是因为你没有实现 数据源和代理方法 实现下就好了 func tableView(_ tableView: UITableView, numberOfRowsInSection section: In ...

  5. [iOS]swift之UITableView添加通过xib创建的headerView坑爹问题

    情景是这样的,我UITableView添加了一个HeaderView,这个HeaderView是通过xib创建,是UIView.出来的结果却出乎意料,UITableView的Cell最顶部的几个被He ...

  6. Swift - UIView,UItableView,Cell设置边框方法

    // 设置边框的宽度 cell.layer.borderWidth = 1 // 设置边框的颜色 cell.layer.borderColor = UIColor.blackColor().CGCol ...

  7. swift实现UItableview上拉下拉刷新模块

    最近用写个项目 发现上拉下拉刷新模块没找到合适的 so 自己写了一个 由于最近忙 教程就不写了 里面有 直接贴地址https://github.com/DaChengTechnology/DCRefr ...

  8. iOS开发——高级UI&带你玩转UITableView

    带你玩装UITableView 在实际iOS开发中UITableView是使用最多,也是最重要的一个控件,如果你不会用它,那别说什么大神了,菜鸟都不如. 其实关于UItableView事非常简单的,实 ...

  9. swift系统学习控件篇:UITableView+UICollectionView

    工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码: UITableView: // // ViewController.swift // UIt ...

随机推荐

  1. iOS:沙盒、偏好设置、归档、解归档

    一.沙盒和应用程序包 •iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被称为沙盒 •iOS常用目录: –Bundle –Documents –Library/Ca ...

  2. PHP的错误处理方式(开发和上线)

    对于PHP开发人员来说,一旦某个产品投入使用.应该马上将 display_errors选项关闭.以免由于这些错误所透露的路径.数据库连接.数据表等信息而遭到黑客攻击. 可是.不论什么一个产品在投入使用 ...

  3. curl教程

    转载自:http://inspire.twgg.org/c/programming/php-curl-teaching.html 目前最完整的CURL中文说明,学PHP的要好好掌握,有很多的参数,大部 ...

  4. ES6 中的 Set

    收录待用,修改转载已取得腾讯云授权 作者:kurtshen ES6 新增了几种集合类型,本文主要介绍Set以及其使用. 其基本描述为 Set对象是值的集合,你可以按照插入的顺序迭代它的元素. Set中 ...

  5. RS请求错误之RSV-BBP-0028

    环境:Version  Cognos10.2 单机 非集群 运行报表的时候经常遇到: 错误信息为: The secondary request failed. The requested sessio ...

  6. TeamTalk 5

    TeamTalk 5 Repository for TeamTalk 5 development. Download TeamTalk 5 SDK To build the TeamTalk clie ...

  7. 用Sass创建MetaFizzy效果

    来源:GBin1.com Hugo发现了一个绚丽的css效果应用于web中并教会你如何聪明的重建和使用它.两天前,我看见笔者Hugo在css帮助下重写MetaFizzy的效果,Hugo帮助我们找到了一 ...

  8. Centos 安装Apache软件

    检查rpm    -qa    httpd [root@luozhonghua icons]# rpm    -qa   |grep  httpd httpd-2.2.15-30.el6.centos ...

  9. UVA 10652 Board Wrapping 计算几何

    多边形凸包.. .. Board Wrapping Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu ...

  10. test推荐到极客头条002

    test推荐到极客头条002test推荐到极客头条002test推荐到极客头条002test推荐到极客头条002test推荐到极客头条002test推荐到极客头条002test推荐到极客头条002te ...