Swift - 简单实现一个tableView
来源:互联网 发布:美橙域名管理地址 编辑:程序博客网 时间:2024/06/05 21:54
近期学习了一下swift,对swift做了初步了解,下面将使用swift简单实现一个tableView,如果有什么不全面或者是错误的地方请及时指正,我会及时更正.
通过实现tableView,掌握swift中创建一个工程流程,以及一些和OC不同的地方(Swift很魔性,很多地方要自己手敲,没有代码提示的).
1.创建一个空模板
2.选择语言(Swift)
3.指定根视图(Single View Application可以不用,storyBoard指定根视图)
需要注意的是:
1.swift中不再需要引入头文件,有的只是.swift
2.Swift 和 OC的区别是swift主流是点语法,OC是消息语法
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { self.window = UIWindow(frame: UIScreen.mainScreen().bounds) // Override point for customization after application launch. self.window!.backgroundColor = UIColor.whiteColor() self.window!.makeKeyAndVisible() /// 指定根视图 var main = MainViewController() var navi = UINavigationController(rootViewController: main) self.window!.rootViewController = navi return true }
4.VC实现tableView
以下几点需要注意:
1.Swift签协议之后不会像OC那样人性化提示有哪些协议方法没有实现,不会有警告,有的只是报错,让你误以为自己哪里写错了,坚信自己的思路写下去,补充完协议方法.
2.在cell的使用上和OC有一些区别
下面是代码实现:
import UIKitclass MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { /// 定义tabelView和数组 var tabel:UITableView! var array = ["item1", "item2", "item3", "item4", "item5", "item6", "item7"] override func viewDidLoad() { super.viewDidLoad() /* navi显示标题 */ title = "Swift tableView" /* 定义tableView */ var rect = self.view.frame tabel = UITableView(frame: rect) tabel.registerClass(TableViewCell.self, forCellReuseIdentifier: "reuse") tabel.delegate = self tabel.dataSource = self self.view.addSubview(tabel) // Do any additional setup after loading the view. } /** section 数量 方法 */ func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } /** row 数量 方法 */ func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return array.count } /** row的高度 方法 */ func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 60 } /** tableViewCell方法 */ func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { /// 定义一个cell var str:String = "reuse" var cell:TableViewCell = tableView.dequeueReusableCellWithIdentifier(str, forIndexPath: indexPath) as! TableViewCell if cell.isEqual(nil){ cell = TableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: str) } /** cell赋值 */ cell.title.text = array[indexPath.row] return cell } /** Memory */ override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */}
5.tableViewCell实现
以下几点需要注意:
1.cell实的时候需要重写初始化问题,初始化方法重写的时候可能会报错,报错原因:指定根视图的时候根视图直接使用的是MainViewController()函数,是不带任何参数的,而且程序运行会经过xib文件,会有遗留问题.此时需要添加一段代码解决遗留问题,点击报错红点,自动补齐要填充的代码.
下面是代码实现:
import UIKitclass TableViewCell: UITableViewCell { var title:UILabel! override func awakeFromNib() { super.awakeFromNib() // Initialization code } /** 初始化方法 */ override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) if !self.isEqual(nil){ title = UILabel(frame: CGRectMake(20, 20, 200, 30)) self.contentView.addSubview(title) } } /** 报错修复的 我也不知道是什么鬼 */ required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state }}
6.运行结果
运行结果截图:
0 0
- Swift - 简单实现一个tableView
- Swift 创建一个tableview
- Swift TableView 简单实现冻结窗格功能
- swift之tableview简单创建
- swift tableView的简单用法
- Swift初窥--使用Swift实现TableView
- 用Swift实现一个简单的浏览器
- 用swift创建tableview的简单方法
- swift:ViewController中自定义tableView简单入门
- IOS 实现3D Touch在tableView的简单应用(swift版)
- 用Swift实现一个TableView,列表上调用LeanCloud的数据。
- swift 创建tableView 并实现协议
- 用swift实现tableview的展示
- 简单实现tableView折叠列表
- swift 代理简单实现
- 简单设置tableView动态高度(iOS8 and Swift)
- Swift练习小demo tableView 自定义cell 简单实用
- Swift之旅:1.概述-用Swift实现一个简单的计算器
- java核心技术之reflect(一):一个系统学习reflect的Demo(精)
- 安卓开发艺术探索笔记知识点
- viewpage与scrollview共存,viewpager不显示的解决办法
- vs2008快捷键极其技巧 转载
- Linux Pmap 命令:查看进程用了多少内存
- Swift - 简单实现一个tableView
- C++对象模型
- Reactjs 之 mixins
- Hibernate4探索之旅之前言
- php 将秒数转换为时间(年、天、小时、分、秒)
- Linux下如何修改MySQL物理目录
- uva 1336 - Fixing the Great Wall
- cocos2dx 3.x 实现 A星(A*)(A-star)算法自动寻路(一)
- 由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作(MaxUserPort,TcpTimedWaitDelay)