Xcode 7.3环境下Swift 创建TableView
来源:互联网 发布:朋友圈图片制作软件 编辑:程序博客网 时间:2024/06/05 00:25
一、UITableView创建(分段代码)
1、怎么创建swift的项目这里就不详细写了,直接上代码创建tableview
(1.)遵守tableview的Delegate及DataSource
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource// 定义一个数组 var array = ["兔子0","兔子1","兔子2","兔子3","兔子4","兔子5","兔子6","兔子7","兔子8","兔子9","兔子10","兔子11"]
(2.) 创建tableview,并添加到view上
// 定义一个tableViewvar tableView = UITableView() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. tableView = UITableView(frame: CGRectMake(0, 64, view.bounds.width, view.bounds.height - 64) ,style: UITableViewStyle.Plain) // 在这一步之前 ,先遵守tableview的Delegate及DataSource tableView.delegate = self tableView.dataSource = self // 将tableView添加到View上 self.view.addSubview(tableView)}
(3.) 实现代理方法与数据源方法
// cell 的高度 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 60 } // 返回组数 func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } // 返回行数 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return array.count } // cell的DataSource func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let initIdentifier = "Cell" let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: initIdentifier) // 设置cell的文字 cell.textLabel?.text = array[indexPath.row] // 设置描述文字 cell.detailTextLabel?.text = "baby\(indexPath.row + 1)" // 设置cell 图片 cell.imageView?.image = UIImage(named: "1024") return cell }
二、全部代码
import UIKitclass ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{ var array = ["兔子0","兔子1","兔子2","兔子3","兔子4","兔子5","兔子6","兔子7","兔子8","兔子9","兔子10","兔子11"] func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 60 } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return array.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let initIdentifier = "Cell" let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: initIdentifier) cell.textLabel?.text = array[indexPath.row] cell.detailTextLabel?.text = "baby\(indexPath.row + 1)" cell.imageView?.image = UIImage(named: "1024") return cell } var lableTitle = UILabel() var tableView = UITableView() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. tableView = UITableView(frame: CGRectMake(0, 64, view.bounds.width, view.bounds.height - 64) ,style: UITableViewStyle.Plain) tableView.delegate = self tableView.dataSource = self self.view.addSubview(tableView) lableTitle = UILabel() lableTitle.text = "TableView" lableTitle.textColor = UIColor .redColor() lableTitle.textAlignment = NSTextAlignment.Center lableTitle.frame = CGRectMake(0, 0, view.bounds.width, 64) lableTitle.backgroundColor = UIColor .grayColor() self.view.addSubview(lableTitle) }}
三、上一个预览图
1 0
- Xcode 7.3环境下Swift 创建TableView
- Xcode 7.3环境下Swift隐藏状态栏
- swift创建tableview
- Swift 创建一个tableview
- tableview创建(swift)
- swift创建tableview
- swift之tableview简单创建
- 用swift创建tableview的简单方法
- 1分钟用swift创建tableview
- swift 创建tableView 并实现协议
- Mac下cocos2dx-3.2+Xcode环境配置和项目创建
- Mac下cocos2dx-3.2+Xcode环境配置和项目创建
- swift 里面tableview的cell代码创建方法
- Swift UITableView相关功能(一)创建tableView
- swift实现微信通讯录列表TableView的创建
- Swift学习记录 -- 15.tableView的创建与基本操作
- XCode下Swift – WebView IOS demo
- swift下tableview的使用(cell点击伸缩效果)
- 计算机网络的三种通讯模式(单播,广播,组播)小结
- sql server的某些表
- h5学习笔记: 下拉菜单
- Java:继承
- Swift 函数型参数
- Xcode 7.3环境下Swift 创建TableView
- 第一章:Windows程序内部运行机制
- [leetcode] 147. Insertion Sort List
- A31S红外遥控
- Change Not For Replication Value for SQL Server Identity Columns
- MVP架构快速搭建Dagger
- 一名女产品经理的工作状态
- POJ 1125 Stockbroker Grapevine
- 对于一个m*n的整数矩阵,其中每一行和每一列的元素都按升序排列,设计一个高效的算法判断一个数值是否存在,并给出位置