UITableView控件使用

使用UITableView,在控件库中,拖拽一个Table View到ViewController中,在Controller的后台代码中需要继承UITableViewDelegate和UITableViewDataSource的协议。
重写方法
tableView(_:numberOfRowsInSection)
此方法是UITableViewDataSource协议的方法,返回tableView加载的行数。
实例代码
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
//返回table中每个节点行数,在tableView中还可以使用节,就是组。后续介绍组的用法
return restaurantNames.count
}
tableView(_:cellForRowAtIndexPath)
此方法是UITableViewDatSource协议的方法,该方法中实现如何加载TableView中的数据。
实例代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {
//这里是的Cell是在Storyboard中设置的tableViewCell的Identifier
let cellIdentifier = "Cell"
//这就是获取单元格
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:
indexPath) as UITableViewCell
// Configure the cell...
cell.textLabel.text = restaurantNames[indexPath.row]
return cell
}
dequeueReusableCellWithIdentifier:从queue中取回一个名称是[identifier]的可用的单元格。这里为什么是从列队中取:看如下解释:

设置dataSource和delegate

两种方式设置tableView的dataSource和delegate

  1. 在Storyboard中右键选择tableView


将dataSource和delegate拖动到当前的ViewController上

  1. 在Controller的代码中,链接tableView

@IBOutlet var tableView: UITableView!
然后再viewDidLoad的方法中设置tableView的dataSource和delegate
tableView.dataSource = self
tableView.delegate = self

设置单元格的缩略图

在tableView(_:cellForRowAtIndexpath)的方法中
cell.imageView.image = UIImage(name:"imagename")

隐藏状态栏

override func prefersStatusBarHidden() -> Bool {
return true
}

自定义单元格

修改Custom属性

在storyboard中选择tableViewCell,在属性索引器中,将Style的属性变更为Custom

修改tableView行高

修改单元格行高

自定义单元格样式

在控件库中,可以拖拽控件到单元格中拜访出自己想要的格式

为自定义单元格创建类文件

在工程中添加新文件(command + N),选择Cocoa Touch Class,在SubClass of中选择UITableViewCell,不需要xib文件。
在类文件中定义控件
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var locationLabel: UILabel!
@IBOutlet weak var typeLabel: UILabel!
@IBOutlet weak var thumbnailImageView: UIImageView!

关联类文件和控件定义

设置tableViewCell的class为新建的class

设置控件关联

关联后结果

修改获取单元格的方法

在tableView(_:cellForRowAtIndexpath)的方法中,把原来的获取单元格的方法修改
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:
indexPath) as UITableViewCell
修改后
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:
indexPath) as CustomTableViewCell
CustomTableViewCell就是我们新定义的类
设置单元格
cell.nameLabel.text = restaurantNames[indexPath.row]
cell.thumbnailImageView.image = UIImage(named: restaurantImages[indexPath.row])

如何设置图片圆角

代码实现:
cell.thumbnailImageView.layer.cornerRadius = cell.thumbnailImageView.frame.size.width / 2
cell.thumbnailImageView.clipsToBounds = true
clipsToBounds是一个属性开关,只有打开,圆角设置才有效
设置实现:
选择ImageView控件,做如下设置

在User Defined Runtime Attributes中添加key 和 value。 这时不需要设置开关。
同样,我们可以在这里修改backgroundColor,Fond…

单元格选择和UIAlertController

在UITableViewDelegate的协议中,有两个方法
tableView(_:willSelectRowAtIndexpath) 选择前
tableView(_:didSelectRowAtIndexpath) 选择后

设置单元格选中

let cell = tableView.cellForRowAtIndexPath(indexPath)
通过indexPath来获取单元格的时候会产生一个Bug,前面我们有讲过,Cell的加载是通过queue中获取,受queue的印象,在获取cell的时候,会有错位的Bug,解决这个问题的方法是通过控制数据源来解决。
通过设置cell的accessoryType来设置cell的选中状态
cell.accessoryType = UITableViewCellAccessoryType.Checkmark

UIAlertController的使用

实例代码我们写在tableView(_:didSelectRowAtIndexpath)的方法中
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath:
NSIndexPath) {
// Create an option menu as an action sheet
let optionMenu = UIAlertController(title: nil, message: "What do you want to do?",
preferredStyle: .ActionSheet)
// Add actions to the menu
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
optionMenu.addAction(cancelAction)
// Display the menu
self.presentViewController(optionMenu, animated: true, completion: nil)
}
UIAlertControllerStyle有两种,
.Alert
.ActionSheet

1、使用AlertController首先是要创建UIAlertController对象
let optionMenu = UIAlertController(title: nil, message: "What do you want to do?",
preferredStyle: .ActionSheet)
2、然后创建UIAlertAction
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
3、把action添加到Controller中,如果AlertControllerStyole是.ActionSheet,那么在AlertController中可以添加多个Action
optionMenu.addAction(cancelAction)
4、呈现AlertController
// Display the menu
self.presentViewController(optionMenu, animated: true, completion: nil)
在创建action中,有关handler,这里是个委托,可以用闭包实现,也可以做个函数传递函数名称,就是在action点击后触发的委托
let isVisitedAction = UIAlertAction(title: "I've beenhere", style: .Default, handler: {
(action:UIAlertAction!) -> Void in
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.accessoryType = .Checkmark
self.restaurantIsVisited[indexPath.row] = true
})
简化闭包写法
let isVisitedAction = UIAlertAction(title: "I've beenhere", style: .Default) {
//$0表示第一个参数,这里关闭闭包用法可以参考语法笔记
let sender = $0
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.accessoryType = .Checkmark
self.restaurantIsVisited[indexPath.row] = true
}

UIAlertView

UIAlertView类似UIAlertController中的Action,使用相对简单
let alertView = UIAlertView(title:"", message:"", delegate:nil, cancelButtonTitle:"")
alertView.show()

TableRow的删除

在UITableViewDataSource的协议中有个方法
tableView(_:commitEditingStyle:forRowAtIndexPath
在ViewController的类中重写该方法,不做任何实现可以看到如下效果
override func tableView(tableView: UITableView, 
commitEditingStyle editingStyle:UITableViewCellEditingStyle, 
forRowAtIndexPath indexPath: NSIndexPath) {
}

当点击删除按钮,如果要做相关操作,可以在上述方法中实现
实例代码:
override func tableView(tableView: UITableView, commitEditingStyle editingStyle:
UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
self.restaurantNames.removeAtIndex(indexPath.row)
self.restaurantLocations.removeAtIndex(indexPath.row)
self.restaurantTypes.removeAtIndex(indexPath.row)
self.restaurantIsVisited.removeAtIndex(indexPath.row)
self.restaurantImages.removeAtIndex(indexPath.row)
//删除row
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
}
//重新加载tableView
self.tableView.reloadData() 
}

添加RowAction

在IOS 8 SDK中有个新成员UITableViewRowAction,利用这个新成员,我们可以在Row上面有更多的操作

  1. 重写UITableViewDataSource的一个方法tableView(_:editActionsForRowAtIndexPath)
  2. 创建UITableViewRowAction

var shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title:
"Share", handler:nil)

  1. 实现Action后的委托
  2. 返回RowAction

实例代码:
override func tableView(tableView: UITableView, 
editActionsForRowAtIndexPath indexPath:
NSIndexPath) -> [AnyObject] {
var shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title:
"Share", handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
let shareMenu = UIAlertController(title: nil, message: "Share using",
preferredStyle: .ActionSheet) 
let twitterAction = UIAlertAction(title: "Twitter", style:
UIAlertActionStyle.Default, handler: nil)
let facebookAction = UIAlertAction(title: "Facebook", style:
UIAlertActionStyle.Default, handler: nil)
let emailAction = UIAlertAction(title: "Email", style: UIAlertActionStyle.Default,
handler: nil)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel,
handler: nil)
shareMenu.addAction(twitterAction)
shareMenu.addAction(facebookAction)
shareMenu.addAction(emailAction)
shareMenu.addAction(cancelAction)
self.presentViewController(shareMenu, animated: true, completion: nil)
}
)
var deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default,
title: "Delete",handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
// Delete the row from the data source
self.restaurantNames.removeAtIndex(indexPath.row)
self.restaurantLocations.removeAtIndex(indexPath.row)
self.restaurantTypes.removeAtIndex(indexPath.row)
self.restaurantIsVisited.removeAtIndex(indexPath.row)
self.restaurantImages.removeAtIndex(indexPath.row)
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
)
return [deleteAction, shareAction]
}

Beginning iOS 8 Programming with Swift-TableView的更多相关文章

  1. iOS Swift-简单值(The Swift Programming Language)

    iOS Swift-简单值(The Swift Programming Language) 常量的声明:let 在不指定类型的情况下声明的类型和所初始化的类型相同. //没有指定类型,但是初始化的值为 ...

  2. Beginning iOS Programming

    Beginning iOS Programming 2014年 published by Wrox

  3. [iOS翻译]《iOS 7 Programming Cookbook》:iOS文件与文件夹管理(上)

    简介: iOS基于OS X,而OSX本身基于Unix操作系统.在iOS里面,操作系统的完全路径结构是不可见的,因为每个APP的数据都存储自身的沙盒里面.沙盒环境实际上听起来像这样:一个只允许当前APP ...

  4. [iOS翻译]《iOS 7 Programming Pushing the Limits》系列:你可能不知道的Objective-C技巧

    简介: 如果你阅读这本书,你可能已经牢牢掌握iOS开发的基础,但这里有一些小特点和实践是许多开发者并不熟悉的,甚至有数年经验的开发者也是.在这一章里,你会学到一些很重要的开发技巧,但这仍远远不够,你还 ...

  5. Swift技术之如何在iOS 8下使用Swift设计一个自定义的输入法 (主要是NSLayoutConstraint 的使用)

    当前位置: > Swift新手入门 > Swift技术之如何在iOS 8下使用Swift设计一个自定义的输入法 时间:2014-09-10 16:49来源:未知 作者:啊成 举报 点击:5 ...

  6. Professional iOS Network Programming Connecting the Enterprise to the iPhone and iPad

    Book Description Learn to develop iPhone and iPad applications for networked enterprise environments ...

  7. Beginning DirectX11 Game Programming

    DirectX11 or 10 made a big change comparing to DirectX9 The fixed-function pipeline was removed in D ...

  8. iOS圆角view的Swift实现(利用Core Graphics绘制)

    iOS圆角view的Swift实现(利用Core Graphics绘制) 因为app的列表用用到了圆形图片的头像,所以去探究并思考了一下这个问题.首先这个问题有两个方向的解决方案: 把图片弄成圆形的. ...

  9. 李洪强iOS开发之 - 指定刷新tableview的某一组

    李洪强iOS开发之 - 指定刷新tableview的某一组

随机推荐

  1. mysql一机多实例安装记录

    因为想研究mycat,所以需要安装多个mysql实例进行研究.限于没有多于计算机,只能在本机安装了.通过mysql文档,自己琢磨着安装成功! 目录结构如下: 其中one和two文件夹用来模拟数据库分库 ...

  2. 四 Android Capabilities讲解

    本文转自:http://www.cnblogs.com/sundalian/p/5629429.html Android Capabilities讲解   1.Capabilities介绍 可以看下之 ...

  3. Python学习-前台开发-JavaScript、Dom和jQuery

    JavaScript JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. ...

  4. hdu 1085 给出数量限制的母函数问题 Holding Bin-Laden Captive!

    Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  5. 哈希URAL 1941 - Scary Martian Word

    A - Scary Martian Word Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  6. 运维必须掌握的Linux面试题

    1.解释下什么是GPL,GNU,自由软件? GPL:(通用公共许可证):一种授权,任何人有权取得.修改.重新发布自由软件的权力. GNU:(革奴计划):目标是创建一套完全自由.开放的的操作系统. 自由 ...

  7. 【bzoj5056】OI游戏 最短路+矩阵树定理

    题目描述 给出一张无向图,求满足 0号点到所有点的路径长等于原图中它们之间最短路 的生成树的个数. 输入 第一行一个整数N,代表原图结点. 接下来N行,每行N个字符,描绘了一个邻接矩阵.邻接矩阵中, ...

  8. 【bzoj3668】[Noi2014]起床困难综合症 贪心

    原文地址:http://www.cnblogs.com/GXZlegend/p/6797090.html 题目描述 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神 ...

  9. CLion 的 Debug 模式是怎么回事

    我对这个问题产生兴趣的起因是 Codeoforces Avito Challenges 2018 的 E 题,我想到了正解,但写得太慢,最后一刻才提交.有个地方写错,结果是 Runtime error ...

  10. java中的URLEncoder.encode对应JS中用decodeURIComponent,js和java编码,解码

    用get请求传中文,经常搞到乱码,这几天搞搞这个东西,总结一下,以方便以后处理这类的问题. Java代码中的URLEncoder.encode方法和JS的encodeURIComponent功能差不多 ...