swift UITableView(一)
2015-11-09 13:52
323 查看
UITableView 是iOS中很常用的一个控件,下面我们就来实现一下Swift中有关UITableView的创建和使用方法
首先,新建一个项目起名TestTableViewSwift 默认语言选择Swift
接下来我们在默认生成的ViewController.swift 中创建UITableView
首先定义变量
[objc] view
plaincopy
var _tableView:UITableView!
创建 并添加到当前View上
[objc] view
plaincopy
_tableView=UITableView(frame: CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height),style:UITableViewStyle.Plain)
self.view.addSubview(_tableView)
我们运行会发现一张空表出现了
接下来我们给table中添加数据
首先我们准备一个数组
[html] view
plaincopy
var _dataArray:[String]!
[html] view
plaincopy
//准备数据
_dataArray=[String]()
for i in 1...10
{
_dataArray.append("第\(i)行")
}
给ViewController添加一个协议
[html] view
plaincopy
class ViewController: UIViewController,UITableViewDataSource {
将刚才准备的数组添加到tableView上
[html] view
plaincopy
_tableView.dataSource=self
实现两个协议方法
[html] view
plaincopy
//设置每一行的内容
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cellid="cellid"
var cell=tableView.dequeueReusableCellWithIdentifier(cellid) as? UITableViewCell
if cell==nil
{
cell=UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellid)
}
cell!.textLabel?.text=_dataArray![indexPath.row]
return cell!
}
//设置需要展示的行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return _dataArray.count
}
再执行看效果
基本实现了数据展示
首先,新建一个项目起名TestTableViewSwift 默认语言选择Swift
接下来我们在默认生成的ViewController.swift 中创建UITableView
首先定义变量
[objc] view
plaincopy
var _tableView:UITableView!
创建 并添加到当前View上
[objc] view
plaincopy
_tableView=UITableView(frame: CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height),style:UITableViewStyle.Plain)
self.view.addSubview(_tableView)
我们运行会发现一张空表出现了
接下来我们给table中添加数据
首先我们准备一个数组
[html] view
plaincopy
var _dataArray:[String]!
[html] view
plaincopy
//准备数据
_dataArray=[String]()
for i in 1...10
{
_dataArray.append("第\(i)行")
}
给ViewController添加一个协议
[html] view
plaincopy
class ViewController: UIViewController,UITableViewDataSource {
将刚才准备的数组添加到tableView上
[html] view
plaincopy
_tableView.dataSource=self
实现两个协议方法
[html] view
plaincopy
//设置每一行的内容
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cellid="cellid"
var cell=tableView.dequeueReusableCellWithIdentifier(cellid) as? UITableViewCell
if cell==nil
{
cell=UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellid)
}
cell!.textLabel?.text=_dataArray![indexPath.row]
return cell!
}
//设置需要展示的行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return _dataArray.count
}
再执行看效果
基本实现了数据展示
相关文章推荐
- swift UITableView(一)
- swift UIScrollView简单介绍
- swift UIScrollView简单介绍
- swift UITabBarController标签栏控制器
- swift UITabBarController标签栏控制器
- swift 设置 navigation左右两侧按钮
- swift 设置 navigation左右两侧按钮
- swift UINavigationController属性设置
- swift UINavigationController属性设置
- swift uinavigationController 视图控制器切换(二)
- swift uinavigationController 视图控制器切换(二)
- Swift UINavigationController(导航控制器)的创建及导航栏的设置
- Swift UINavigationController(导航控制器)的创建及导航栏的设置
- swift UIViewController生命周期
- swift UIViewController生命周期
- swift UITextFiled边框设置
- swift UITextFiled边框设置
- swift UIImageView 子view接受点击
- swift UIImageView 子view接受点击
- swift UIImageView简单动画