Swift-UITableView的实现

贴代码,于重点处做分析:

class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource{

    

    @IBOutlet var myTable : UITableView

    var items : NSMutableArray = [] //必须初始化

    

    override func viewDidLoad() {

        super.viewDidLoad()

        self.title="God"

        self.setupItems()

        self.setTableView()

        

    }

    

    func setupItems()

    {


        self.items=NSMutableArray()

        for i in 0..100

        {

            items[i]="\(i)"

        }

        

    }

    func setTableView()

    {

        myTable.delegate=self

        myTable.dataSource=self

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    //#prama mark - 表示图代理和数据源

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

    {

        return 100

    }

    

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

    {

        return self.items.count

    }

    

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!

    {

        let cell = tableView .dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

        

        cell.textLabel.text = "\(self.items[indexPath.row])";


        var rLabel = cell.viewWithTag(1) as UILabel

        rLabel.text = "\(items[indexPath.row])"

        //这里 就是viewWithTag的正确用法,必须和addSubview配合

        

        cell.addSubview(rLabel)

        

        

        

        return cell

    }



}


UITableView 实现可以删除的cell:

整体思路:用数组保存数据,配合一些基本的功能。

贴上主要的代码:


 @IBOutlet var myTable : UITableView

    var items = String[]()

这里是确定类型,和初始化,到后面的override func viewDidLoad() 就不用再初始化了。


func setupItems()

    {


        for i in 0..2

        {

            self.items.append("\(i)")

        }

        

    }

append是String[]数组特有的方法,注意类型配合相应的方法,找不到可以command找。


在UITableViewDelegate中,编辑如下方法:

//是否可以编辑行

    func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool

    {

        return true

    }

    

    func tableView(tableView: UITableView!, editingStyleForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCellEditingStyle

    {

        // 枚举类型均可使用 .XX  这种点语法调用

        return .Delete

    }

    func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!)

    {

         // 原理:删除items数组,并删除相应的cell

        self.items.removeAtIndex(indexPath.row)

        //.

        self.myTable?.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

limaning

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值