swift的UITableView的使用
UITableView是app开发中经常使用到的控件,功能非常强大,多用于数据的显示。
以下以一个简单的实例来介绍其基本使用方法。
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的使用的更多相关文章
- iOS开发——实战篇Swift篇&UItableView结合网络请求,多线程,数据解析,MVC实战
UItableView结合网络请求,多线程,数据解析,MVC实战 学了这么久的swift都没有做过什么东西,今天就以自己的一个小小的联系,讲一下,怎么使用swift在实战中应用MVC,并且结合后面的高 ...
- Swift中UITableView的简单使用
Swift中的注释 使用"// MARK:- 注释内容",对属性或方法进行注释 使用"///注释内容"对属性或方法提供调用说明的注释 使用extension对同 ...
- Swift开发UITableView常用的一些细节知识点介绍
<code class="objectivec"><strong><span style="font-size:18px;"> ...
- Swift 给UITableView 写extension 时 报错 does not conform to protocol 'UITableViewDataSource'
那是因为你没有实现 数据源和代理方法 实现下就好了 func tableView(_ tableView: UITableView, numberOfRowsInSection section: In ...
- [iOS]swift之UITableView添加通过xib创建的headerView坑爹问题
情景是这样的,我UITableView添加了一个HeaderView,这个HeaderView是通过xib创建,是UIView.出来的结果却出乎意料,UITableView的Cell最顶部的几个被He ...
- Swift - UIView,UItableView,Cell设置边框方法
// 设置边框的宽度 cell.layer.borderWidth = 1 // 设置边框的颜色 cell.layer.borderColor = UIColor.blackColor().CGCol ...
- swift实现UItableview上拉下拉刷新模块
最近用写个项目 发现上拉下拉刷新模块没找到合适的 so 自己写了一个 由于最近忙 教程就不写了 里面有 直接贴地址https://github.com/DaChengTechnology/DCRefr ...
- iOS开发——高级UI&带你玩转UITableView
带你玩装UITableView 在实际iOS开发中UITableView是使用最多,也是最重要的一个控件,如果你不会用它,那别说什么大神了,菜鸟都不如. 其实关于UItableView事非常简单的,实 ...
- swift系统学习控件篇:UITableView+UICollectionView
工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码: UITableView: // // ViewController.swift // UIt ...
随机推荐
- ASP.NET 5 Beta6发布了(翻译)
感觉就好像我们刚刚发布了一个版本,现在我们又要发布一个新的版本.开发团队通过努力工作在Visual Studio2015的正式版上提交使用ASP.NET 5开发工具的上的更新以及库的更新.新的版本为b ...
- hello--GAN
GAN系列学习(1)——前生今世 DCGAN.WGAN.WGAN-GP.LSGAN.BEGAN原理总结及对比 [Learning Notes]变分自编码器(Variational Auto-Encod ...
- 文字尺寸、宽高的测量 Paint FontMetrics
Paint.FontMetrics类简介 Google文档中的描述: ) throw new IndexOutOfBoundsException(); if (bounds == null) thro ...
- C#特性杂谈
文中充满了各种C#与其他语言的对比及吐槽, 希望介意者勿观… 当然, 鉴于太乱, 我怀疑有没有人能看完. 学习C# Hello World 变量与表达式 动态类型 值类型和引用类型 checked支持 ...
- (转)行为树(Behavior Tree)
转自:http://www.cnblogs.com/konlil/archive/2011/04/23/2025954.html 如果要让游戏里的角色或者NPC能执行预设的AI逻辑,最简单的用IF.. ...
- 解决Ubuntu Ping网关Destination Host Unreachable错误
ifconfig 检查了配置,没问题,是ok的, 检查了防火墙,是关闭的,邪乎了,是什么问题呢 各种尝试,最后,将 ip 搞成自动获取就 可以ping通了,但为啥手动设置就不行呢? 最后看了这个朋友的 ...
- SCP远程拷贝命令
利用scp指令可以在linux机器间传递文件,个人使用几次感觉效率还是较高的,比SFTP+本地做媒介要高得多. 1.本地拷贝到远程. 这种方式的命令模式是scp local_file remote_u ...
- Ajax datatype:'JSON'的error问题Status1:200,JSON格式
转自:http://blog.sina.com.cn/s/blog_6e001be701017rux.html <script src="../js/jquery-1.8.0-vsdo ...
- .NET反编译之manager,base.AutoScaleMode修复
使用反编译软件导出项目时,出现警告:设计器无法处理第X 行的代码:this.AutoScaleMode = AutoScaleMode.Font;方法"InitializeComponent ...
- MySQL 中now()时间戳用法
MySQL 中now()时间戳用法 UPDATE news set addtime = unix_timestamp(now()); #结果:1452001082