您的位置:首页 > 移动开发 > Swift

swift UITableView具体使用方法

2016-09-18 08:48 369 查看
 

研究了一下tableview的基本功能写了个dome

稍后附上git地址

 

 

效果

 


  


   

代码

 

//

// 
ViewController.swift

// 
tableview

//

//  Created
by admin on 16/6/2.

// 
Copyright © 2016年

ming. All rights reserved.

//

 

import
UIKit

 

class
ViewController:
UIViewController,UITableViewDataSource,UITableViewDelegate{

 

   

//屏幕的宽和高

   

let
width =
UIScreen.mainScreen().bounds.width

   

let
height =
UIScreen.mainScreen().bounds.height

 

   

let
top:CGFloat

=
30

   

   

@IBOutlet
weak
var
list:
UITableView!

   

   

let
str = ["这是标题1","这是标题2","这是标题3"]

   

   

   

override
funcviewDidLoad()
{

       

super.viewDidLoad()

       

// Do any additional setup after loading the view, typically from a
nib.

       

       

self.list.delegate

=
self

       

self.list.dataSource

=
self

       

       

let
titleFrame:CGRect

=
CGRectMake(0,

top,

width,

height)

       

self.list.frame

= titleFrame

       

   
}

 

   

override
funcdidReceiveMemoryWarning()
{

       

super.didReceiveMemoryWarning()

       

// Dispose of any resources that can be recreated.

   
}

   

//
返回表格行数(也就是返回控件数)

   

func
tableView(tableView:
UITableView,
numberOfRowsInSection section:
Int)
->
Int
{

       

return
str.count

   
}

   

   

//
创建各单元显示内容(创建参数indexPath指定的单元)

   

func
tableView(tableView:
UITableView,
cellForRowAtIndexPath indexPath:
NSIndexPath)

       
->
UITableViewCell

   
{

       

let
h =
height/10

       

let
titleFrame:CGRect

=
CGRectMake(0,

0,

width/2,
h)

       

let
cellFrame:CGRect

=
CGRectMake(0,

0,

width,
h)

       

       

//
为了提供表格显示性能,已创建完成的单元需重复使用

       

let
identify:String

=
"TableViewCell"

       

//
同一形式的单元格重复使用,在声明时已注册

       

let
cell =
list.dequeueReusableCellWithIdentifier(identify,forIndexPath:
indexPath)
as!

TableViewCell

       

       
cell.title.text

=
str[indexPath.row]

       
cell.title.frame

= titleFrame

       
cell.frame

= cellFrame

       

return
cell

   
}

   

   

func
tableView(tableView:
UITableView,
didSelectRowAtIndexPath indexPath:
NSIndexPath){

       

alert(str[indexPath.row])

   
}

   

//侧滑选项

   

func
tableView(tableView:
UITableView,
editActionsForRowAtIndexPath indexPath:
NSIndexPath)
-> [UITableViewRowAction]?

   
{

       

let
a =
UITableViewRowAction(style:
.Normal,
title:
"选项一")
{ action, index
in

           

self.alert("选项一")

       
}

       

       
a.backgroundColor

=
UIColor.redColor()

       

       

let
b =
UITableViewRowAction(style:
.Normal,
title:
"选项2")
{ (action:
UITableViewRowAction!,
indexPath:
NSIndexPath)
->
Void
in

           

self.alert("选项二")

       
}

       

       
b.backgroundColor

=
UIColor.grayColor()

       

       

let
c =
UITableViewRowAction(style:
.Normal,
title:
"选项3")
{ action, index
in

           

self.alert("选项三")

       
}

       
c.backgroundColor

=
UIColor.blueColor()

       

       

return
[a, b, c]

   
}

   

//返回cell高度

   

func
tableView(tableView:
UITableView,
heightForRowAtIndexPath indexPath:
NSIndexPath)
->
CGFloat
{

       

       

return
height/10

   
}

   

   

func
alert(str:String){

       

       

//提示窗

       

let
alertViewController:UIAlertController

=
UIAlertController(title:"提示",
message:str, preferredStyle:
UIAlertControllerStyle.Alert)

       

let
alertView =
UIAlertAction(title:

"OK",
style:
UIAlertActionStyle.Default,
handler:
nil)

       
alertViewController.addAction(alertView)

       

self.presentViewController(alertViewController,
animated:
true,
completion:
nil)

   
}

 

}

 

 

 
 

 

TableViewCell代码

 

import
UIKit

 

class
TableViewCell:
UITableViewCell
{

 

 

   

@IBOutlet
weak
var
title:
UILabel!

   

   

override
func
awakeFromNib() {

       

super.awakeFromNib()

       

// Initialization code

   
}

 

   

override
func
setSelected(selected:
Bool,
animated:
Bool)
{

       

super.setSelected(selected,
animated: animated)

 

       

// Configure the view for the selected state

   
}

 

}
 

补充
设置cell没有点击效果

cell.selectionStyle = UITableViewCellSelectionStyle.None
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: