swift总结2
2015-09-01 14:56
519 查看
1/ 创建一个 可变数组
var dataList:
NSMutableArray = ["qqqqq","wwwwwwww","eeeeeee","rrrrrrrrrr","tttttttt","yyyyyyyyyy","uuuuuuuuuuuu","iiiiiiiiiiii","oooooooooooo","ppppppppppp","aaaaaaaaaa","ssssssssss","ddddddddddd","ffffffffff","ggggggggggggg","hhhhhhhhhhhhh"];
2/ 闭包 相当于 OC 中的 block
var zyTableView:
UITableView =
{
var tempTableView =
UITableView (frame: self.view.bounds)
tempTableView.delegate =
self
tempTableView.dataSource =
self
return tempTableView
}()
3/ cell 的定义 及 cell 左滑 cell 移动
func tableView(tableView:
UITableView, cellForRowAtIndexPath indexPath:
NSIndexPath) -> UITableViewCell
{
let identifier: String = "Cell"
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell")
if !(cell != nil) {
var cell = UITableViewCell (style:
UITableViewCellStyle.Subtitle, reuseIdentifier:
"cell")
}
cell.textLabel?.text =
self.dataList[indexPath.row]
as? String
return cell
}
func tableView(tableView:
UITableView, commitEditingStyle editingStyle:
UITableViewCellEditingStyle, forRowAtIndexPath indexPath:
NSIndexPath) {
if(editingStyle ==
UITableViewCellEditingStyle.Delete){
dataList.removeObjectAtIndex(indexPath.row)
tableView .deleteRowsAtIndexPaths([indexPath], withRowAnimation:
UITableViewRowAnimation.Middle)
}else
if(editingStyle ==
UITableViewCellEditingStyle.Insert){
dataList .insertObject("章鱼哥", atIndex: indexPath.row+1)
let zyIndexPath =
NSIndexPath (forRow: indexPath.row+1, inSection: indexPath.section)
zyTableView .insertRowsAtIndexPaths([zyIndexPath], withRowAnimation:
UITableViewRowAnimation.Middle)
}
}
func tableView(tableView:
UITableView, editingStyleForRowAtIndexPath indexPath:
NSIndexPath) -> UITableViewCellEditingStyle {
if(indexPath.row %
2 == 1){
return
UITableViewCellEditingStyle.Insert
}
else{
return
UITableViewCellEditingStyle.Delete
}
}
func tableView(tableView:
UITableView, moveRowAtIndexPath sourceIndexPath:
NSIndexPath, toIndexPath destinationIndexPath:
NSIndexPath) {
let source: AnyObject =
self.dataList[sourceIndexPath.row]
dataList .removeObjectAtIndex(sourceIndexPath.row)
dataList .insertObject(source, atIndex: destinationIndexPath.row)
}
4/ UIScrollview
var zyScrollView:
UIScrollView = {
var tempScrollView:
UIScrollView = UIScrollView()
tempScrollView.frame =
self.view.frame
tempScrollView.delegate =
self
//设置边距
tempScrollView.contentInset =
UIEdgeInsetsMake(20,
20, 20,
20)
//不显示水平滚动标示
tempScrollView.showsHorizontalScrollIndicator =
false
//不显示垂直滚动标示
tempScrollView.showsVerticalScrollIndicator =
false
//设置偏移
// ***
偏移位置
tempScrollView.contentOffset =
CGPointMake(0, -100)
//
取消弹簧效果,内容固定,不希望出现弹簧效果时
//
不要跟bounds属性搞混了
tempScrollView.bounces =
false
//
设置最大/最小缩放比例
tempScrollView.minimumZoomScale =
CGFloat(0.2)
tempScrollView.maximumZoomScale =
CGFloat(2.0)
tempScrollView.backgroundColor =
UIColor .cyanColor()
return tempScrollView
}()
5/
//
让图像视图根据图像自动调整大小
tempImage .sizeToFit()
6/ UIScrollview 代理方法 缩放
/**
1> 设置了代理
2> 指定了最大、最小的缩放比例
表示ScrollView是可以缩放的
代理方法的"返回值"实际上就是控制器告诉滚动视图,要缩放的是UIImageView
*/
//
告诉ScrollView要缩放的视图是谁,具体的缩放实现,是由ScrollView来完成的
// 1> scrollView要知道缩放谁
func viewForZoomingInScrollView(scrollView:
UIScrollView) -> UIView? {
return self.zyImageView;
}
// 2>
滚动视图即将开始缩放,通常不需要写
func scrollViewWillBeginZooming(scrollView:
UIScrollView, withView view: UIView?) {
print(__FUNCTION__)
}
// 3>
正在缩放,通常也不需要实现
func scrollViewDidZoom(scrollView:
UIScrollView) {
print(__FUNCTION__)
print(NSStringFromCGAffineTransform(zyImageView.transform))
}
func scrollViewDidEndZooming(scrollView:
UIScrollView, withView view: UIView?, atScale scale:
CGFloat) {
print(__FUNCTION__)
}
7/ 可变高度的cell 需要用代码创建
(1) 在tableview的控制器中 添加 XXFrames的可变数组
var messageFrames:
NSMutableArray = {
let array: NSArray =
NSArray (contentsOfFile:
NSBundle .mainBundle() .pathForResource("messages.plist", ofType:
nil)! )!
var arrayM:
NSMutableArray = NSMutableArray()
var dictionary: NSDictionary =
NSDictionary()
for dictionary in a
1164b
rray{
var message: ZYMessage =
ZYMessage(dictionary: dictionary
as! NSDictionary)
let lastFm: ZYMessageFrame? = arrayM.lastObject
as? ZYMessageFrame
let string: NSString = message.time
let messageFrame:
ZYMessageFrame = ZYMessageFrame()
messageFrame.message = message
arrayM.addObject(messageFrame)
}
return arrayM
}()
(2)XXFrames 数组存的事 XXFrame元素 (含有 XX 的模型数据)
(3)定义XX 模型数据
class ZYMessage:
NSObject {
override
init(){
super.init()
}
// 添加的属性 需要在cell中 展示的数据
var text:NSString =
""
var time:NSString =
""
init(dictionary: NSDictionary) {
super.init()
text = dictionary .objectForKey("text")
as! NSString
time = dictionary .objectForKey("time")
as!
NSString
}
class func messageWithDict(dictionary:
NSDictionary) -> ZYMessage{
return ZYMessage(dictionary: dictionary)
}
}
(4)定义XXFrame 类
class ZYMessageFrame:
NSObject {
// 分别定义cell中 各控件的 frame
let padding: CGFloat =
10
let textPadding: CGFloat =
20
var textF:
CGRect = CGRectZero
var timeF:
CGRect = CGRectZero
var iconF:
CGRect = CGRectZero
var cellHeight: CGFloat =
0
let maxF =
CGFloat(MAXFLOAT)
var message: ZYMessage? =
nil{
didSet{
timeF =
CGRectMake(timeX, timeY, timeW, timeH)
iconF = CGRectMake(iconX, iconY, iconW, iconH)
//根据文字长度求 宽高
let textSize = message!.text.boundingRectWithSize(CGSizeMake(150,
maxF), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont .systemFontOfSize(15)],
context: nil).size
//4. cell的高度
let iconMaxY = CGRectGetMaxY(iconF)
let textMaxY = CGRectGetMaxY(textF)
cellHeight = max(iconMaxY, textMaxY)
}
}
}
(5)在控制器中 返回的cell高度
func tableView(tableView:
UITableView, heightForRowAtIndexPath indexPath:
NSIndexPath) -> CGFloat {
let messageFrame:
ZYMessageFrame = self.messageFrames[indexPath.row]
as! ZYMessageFrame
return messageFrame.cellHeight
}
(6)在cell中传入的数据 也是 XXFrame 而不是 XX数据模型
func tableView(tableView:
UITableView, cellForRowAtIndexPath indexPath:
NSIndexPath) -> UITableViewCell {
var cell:ZYTalkTableViewCell? = tableView.dequeueReusableCellWithIdentifier(ID)
as?
ZYTalkTableViewCell
if (cell == nil) {
cell =
ZYTalkTableViewCell (style:
UITableViewCellStyle.Default, reuseIdentifier:
ID)
}
cell?.messageFrame =
messageFrames[indexPath.row]
as!
ZYMessageFrame
return cell!
}
(7)自定义cell
class ZYTalkTableViewCell:
UITableViewCell {
let textPadding: CGFloat =
20
var timeLabel:
UILabel =
UILabel()
var icomImageView:
UIImageView = UIImageView ()
var button: UIButton =
UIButton ()
var messageFrame:
ZYMessageFrame = ZYMessageFrame(){
didSet{
let message:
ZYMessage = messageFrame.message!
//1. 时间
timeLabel.frame =
messageFrame.timeF
timeLabel.text = message.time
as String
icomImageView.frame =
messageFrame.iconF
//3. 正文
button .setTitle(message.text
as String, forState:
UIControlState.Normal)
button.frame =
messageFrame.textF
}
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code 加载XIB后的初始化代码
}
override func setSelected(selected:
Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure theb view for the selected state
}
按照这个样式来写 初始化方法
override init(style:
UITableViewCellStyle, reuseIdentifier:
String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//1. 时间
timeLabel.textAlignment =
NSTextAlignment.Center
timeLabel.font =
UIFont .systemFontOfSize(13.0)
self.contentView .addSubview(timeLabel)
//2. 头像
self.contentView .addSubview(icomImageView)
//3. 正文
button.titleLabel?.font =
UIFont .systemFontOfSize(15.0)
button.titleLabel?.numberOfLines =
0
button.setTitleColor(UIColor .blackColor(), forState:
UIControlState.Normal)
button.contentEdgeInsets =
UIEdgeInsetsMake(textPadding,
textPadding, textPadding,
textPadding)
self.contentView .addSubview(button)
self.backgroundColor =
UIColor .clearColor()
}
这个函数 必须加
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func resizeImageWithName(name:
String) -> UIImage{
let image:
UIImage = UIImage (named : name)!
let w = image.size.width *
0.5 - 1
let h = image.size.height *
0.5 - 1
return image .resizableImageWithCapInsets(UIEdgeInsetsMake(h, w, h, w))
}
}
var dataList:
NSMutableArray = ["qqqqq","wwwwwwww","eeeeeee","rrrrrrrrrr","tttttttt","yyyyyyyyyy","uuuuuuuuuuuu","iiiiiiiiiiii","oooooooooooo","ppppppppppp","aaaaaaaaaa","ssssssssss","ddddddddddd","ffffffffff","ggggggggggggg","hhhhhhhhhhhhh"];
2/ 闭包 相当于 OC 中的 block
var zyTableView:
UITableView =
{
var tempTableView =
UITableView (frame: self.view.bounds)
tempTableView.delegate =
self
tempTableView.dataSource =
self
return tempTableView
}()
3/ cell 的定义 及 cell 左滑 cell 移动
func tableView(tableView:
UITableView, cellForRowAtIndexPath indexPath:
NSIndexPath) -> UITableViewCell
{
let identifier: String = "Cell"
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell")
if !(cell != nil) {
var cell = UITableViewCell (style:
UITableViewCellStyle.Subtitle, reuseIdentifier:
"cell")
}
cell.textLabel?.text =
self.dataList[indexPath.row]
as? String
return cell
}
func tableView(tableView:
UITableView, commitEditingStyle editingStyle:
UITableViewCellEditingStyle, forRowAtIndexPath indexPath:
NSIndexPath) {
if(editingStyle ==
UITableViewCellEditingStyle.Delete){
dataList.removeObjectAtIndex(indexPath.row)
tableView .deleteRowsAtIndexPaths([indexPath], withRowAnimation:
UITableViewRowAnimation.Middle)
}else
if(editingStyle ==
UITableViewCellEditingStyle.Insert){
dataList .insertObject("章鱼哥", atIndex: indexPath.row+1)
let zyIndexPath =
NSIndexPath (forRow: indexPath.row+1, inSection: indexPath.section)
zyTableView .insertRowsAtIndexPaths([zyIndexPath], withRowAnimation:
UITableViewRowAnimation.Middle)
}
}
func tableView(tableView:
UITableView, editingStyleForRowAtIndexPath indexPath:
NSIndexPath) -> UITableViewCellEditingStyle {
if(indexPath.row %
2 == 1){
return
UITableViewCellEditingStyle.Insert
}
else{
return
UITableViewCellEditingStyle.Delete
}
}
func tableView(tableView:
UITableView, moveRowAtIndexPath sourceIndexPath:
NSIndexPath, toIndexPath destinationIndexPath:
NSIndexPath) {
let source: AnyObject =
self.dataList[sourceIndexPath.row]
dataList .removeObjectAtIndex(sourceIndexPath.row)
dataList .insertObject(source, atIndex: destinationIndexPath.row)
}
4/ UIScrollview
var zyScrollView:
UIScrollView = {
var tempScrollView:
UIScrollView = UIScrollView()
tempScrollView.frame =
self.view.frame
tempScrollView.delegate =
self
//设置边距
tempScrollView.contentInset =
UIEdgeInsetsMake(20,
20, 20,
20)
//不显示水平滚动标示
tempScrollView.showsHorizontalScrollIndicator =
false
//不显示垂直滚动标示
tempScrollView.showsVerticalScrollIndicator =
false
//设置偏移
// ***
偏移位置
tempScrollView.contentOffset =
CGPointMake(0, -100)
//
取消弹簧效果,内容固定,不希望出现弹簧效果时
//
不要跟bounds属性搞混了
tempScrollView.bounces =
false
//
设置最大/最小缩放比例
tempScrollView.minimumZoomScale =
CGFloat(0.2)
tempScrollView.maximumZoomScale =
CGFloat(2.0)
tempScrollView.backgroundColor =
UIColor .cyanColor()
return tempScrollView
}()
5/
//
让图像视图根据图像自动调整大小
tempImage .sizeToFit()
6/ UIScrollview 代理方法 缩放
/**
1> 设置了代理
2> 指定了最大、最小的缩放比例
表示ScrollView是可以缩放的
代理方法的"返回值"实际上就是控制器告诉滚动视图,要缩放的是UIImageView
*/
//
告诉ScrollView要缩放的视图是谁,具体的缩放实现,是由ScrollView来完成的
// 1> scrollView要知道缩放谁
func viewForZoomingInScrollView(scrollView:
UIScrollView) -> UIView? {
return self.zyImageView;
}
// 2>
滚动视图即将开始缩放,通常不需要写
func scrollViewWillBeginZooming(scrollView:
UIScrollView, withView view: UIView?) {
print(__FUNCTION__)
}
// 3>
正在缩放,通常也不需要实现
func scrollViewDidZoom(scrollView:
UIScrollView) {
print(__FUNCTION__)
print(NSStringFromCGAffineTransform(zyImageView.transform))
}
func scrollViewDidEndZooming(scrollView:
UIScrollView, withView view: UIView?, atScale scale:
CGFloat) {
print(__FUNCTION__)
}
7/ 可变高度的cell 需要用代码创建
(1) 在tableview的控制器中 添加 XXFrames的可变数组
var messageFrames:
NSMutableArray = {
let array: NSArray =
NSArray (contentsOfFile:
NSBundle .mainBundle() .pathForResource("messages.plist", ofType:
nil)! )!
var arrayM:
NSMutableArray = NSMutableArray()
var dictionary: NSDictionary =
NSDictionary()
for dictionary in a
1164b
rray{
var message: ZYMessage =
ZYMessage(dictionary: dictionary
as! NSDictionary)
let lastFm: ZYMessageFrame? = arrayM.lastObject
as? ZYMessageFrame
let string: NSString = message.time
let messageFrame:
ZYMessageFrame = ZYMessageFrame()
messageFrame.message = message
arrayM.addObject(messageFrame)
}
return arrayM
}()
(2)XXFrames 数组存的事 XXFrame元素 (含有 XX 的模型数据)
(3)定义XX 模型数据
class ZYMessage:
NSObject {
override
init(){
super.init()
}
// 添加的属性 需要在cell中 展示的数据
var text:NSString =
""
var time:NSString =
""
init(dictionary: NSDictionary) {
super.init()
text = dictionary .objectForKey("text")
as! NSString
time = dictionary .objectForKey("time")
as!
NSString
}
class func messageWithDict(dictionary:
NSDictionary) -> ZYMessage{
return ZYMessage(dictionary: dictionary)
}
}
(4)定义XXFrame 类
class ZYMessageFrame:
NSObject {
// 分别定义cell中 各控件的 frame
let padding: CGFloat =
10
let textPadding: CGFloat =
20
var textF:
CGRect = CGRectZero
var timeF:
CGRect = CGRectZero
var iconF:
CGRect = CGRectZero
var cellHeight: CGFloat =
0
let maxF =
CGFloat(MAXFLOAT)
var message: ZYMessage? =
nil{
didSet{
timeF =
CGRectMake(timeX, timeY, timeW, timeH)
iconF = CGRectMake(iconX, iconY, iconW, iconH)
//根据文字长度求 宽高
let textSize = message!.text.boundingRectWithSize(CGSizeMake(150,
maxF), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont .systemFontOfSize(15)],
context: nil).size
//4. cell的高度
let iconMaxY = CGRectGetMaxY(iconF)
let textMaxY = CGRectGetMaxY(textF)
cellHeight = max(iconMaxY, textMaxY)
}
}
}
(5)在控制器中 返回的cell高度
func tableView(tableView:
UITableView, heightForRowAtIndexPath indexPath:
NSIndexPath) -> CGFloat {
let messageFrame:
ZYMessageFrame = self.messageFrames[indexPath.row]
as! ZYMessageFrame
return messageFrame.cellHeight
}
(6)在cell中传入的数据 也是 XXFrame 而不是 XX数据模型
func tableView(tableView:
UITableView, cellForRowAtIndexPath indexPath:
NSIndexPath) -> UITableViewCell {
var cell:ZYTalkTableViewCell? = tableView.dequeueReusableCellWithIdentifier(ID)
as?
ZYTalkTableViewCell
if (cell == nil) {
cell =
ZYTalkTableViewCell (style:
UITableViewCellStyle.Default, reuseIdentifier:
ID)
}
cell?.messageFrame =
messageFrames[indexPath.row]
as!
ZYMessageFrame
return cell!
}
(7)自定义cell
class ZYTalkTableViewCell:
UITableViewCell {
let textPadding: CGFloat =
20
var timeLabel:
UILabel =
UILabel()
var icomImageView:
UIImageView = UIImageView ()
var button: UIButton =
UIButton ()
var messageFrame:
ZYMessageFrame = ZYMessageFrame(){
didSet{
let message:
ZYMessage = messageFrame.message!
//1. 时间
timeLabel.frame =
messageFrame.timeF
timeLabel.text = message.time
as String
icomImageView.frame =
messageFrame.iconF
//3. 正文
button .setTitle(message.text
as String, forState:
UIControlState.Normal)
button.frame =
messageFrame.textF
}
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code 加载XIB后的初始化代码
}
override func setSelected(selected:
Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure theb view for the selected state
}
按照这个样式来写 初始化方法
override init(style:
UITableViewCellStyle, reuseIdentifier:
String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//1. 时间
timeLabel.textAlignment =
NSTextAlignment.Center
timeLabel.font =
UIFont .systemFontOfSize(13.0)
self.contentView .addSubview(timeLabel)
//2. 头像
self.contentView .addSubview(icomImageView)
//3. 正文
button.titleLabel?.font =
UIFont .systemFontOfSize(15.0)
button.titleLabel?.numberOfLines =
0
button.setTitleColor(UIColor .blackColor(), forState:
UIControlState.Normal)
button.contentEdgeInsets =
UIEdgeInsetsMake(textPadding,
textPadding, textPadding,
textPadding)
self.contentView .addSubview(button)
self.backgroundColor =
UIColor .clearColor()
}
这个函数 必须加
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func resizeImageWithName(name:
String) -> UIImage{
let image:
UIImage = UIImage (named : name)!
let w = image.size.width *
0.5 - 1
let h = image.size.height *
0.5 - 1
return image .resizableImageWithCapInsets(UIEdgeInsetsMake(h, w, h, w))
}
}
相关文章推荐
- 最老程序员创业开发实训5---Swift语言极速入门
- 相亲神器记录Swift1.2
- swift总结1
- swift自学注意点
- swift 中#pragma mark 表示方法
- swift详解之二十--------在xcode中使用git管理源代码,将代码提交到github
- swift 添加按钮
- swift - The Chain Responsibility pattern
- swift详解之十九--------------UITableView的基本操作(下拉刷新,新增删除,分组,检索等)
- IOS--Swift 01
- Swift UILabel 文字大小随着宽度调整
- Swift UILabel常用功能
- Swift子类初始化函数中调用父类初始化函数的正确位置
- swift 创建单例模式
- 【IOS-Swift】实战笔记——数据持久化之NSUserDefaults(本地轻量级数据存储)
- 【IOS-Swift】实战笔记——数据持久化之归档NSKeyedArchiver
- [iOS]#Swift#OC+Swift混编
- JAVA人学习ios swift(笔记)
- Swift2.0发布以来的历次主要更新
- Swift UIview简单动画