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
相关文章推荐
- IOS_Swift1之宏、闭包、UITableView、内部变量、类方法的使用
- MKMapView的使用 和 uitableview的…
- UITableView reloadData的正确方法
- UITableView 的全部代理方法
- [转]vim ctags使用方法
- 转 UITableView 基本使用方法总结
- (转载) UITableView 系列一 :基本使用方法 (显示,删除,添加图片,添加样式等
- UITableView 基本使用方法总结
- UITableView 基本使用方法总结
- UITableView 基本使用方法总结
- UITableView 基本使用方法总结
- UITableView 基本使用方法总结
- UITableView 基本使用方法总结
- UITableView 系列一 :基本使用方法 (显示,删除,添加图片,添加样式等) (实例)
- UITableView 基本使用方法总结
- Android WebView的使用
- UITableView 基本使用方法总结
- UITableView 基本使用方法
- 转:UITableView 基本使用方法总结
- UITableView 基本使用方法总结