swift UITableView具体使用方法

 

研究了一下tableview的基本功能写了个dome 稍后附上git地址

 

 

效果

 

swift <wbr>UITableView具体使用方法   swift <wbr>UITableView具体使用方法   

代码

 

//

//  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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值