swift之UITableView

来源:互联网 发布:丹尼尔戴刘易斯 知乎 编辑:程序博客网 时间:2024/06/05 15:42

UITableView几乎是每个APP中必用的控件之一,到处都能见到它的身影。本文所讲的是swift版的实现。

协议

class MoreViewController: RootViewController,UITableViewDataSource,UITableViewDelegate

注:写好协议可能会报错,等实现了两个必选协议方法后就不会了


var tableView : UITableView?

var titleArr = NSArray(objects: "修改登录密码","帮助中心","关于我们","意见反馈","联系我们");


//实现

func initTableView()

    {

        self.tableView =UITableView(frame: self.view.frame, style: .Plain);

        self.tableView?.backgroundColor =RGBA(246, g:247, b: 249, a:1.0);

        self.tableView?.dataSource =self;

        self.tableView?.delegate =self;

        self.view.addSubview(self.tableView!);

    }


    //返回行数

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

        return self.titleArr.count;

    }

    

    //定制cell

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

        

        let cellName = "MoreCellName";

        

        //MoreViewCell为自定义cell

        var cell = tableView.dequeueReusableCellWithIdentifier(cellName)as? MoreViewCell

        if cell == nil{

            

            cell = MoreViewCell(style: .Default, reuseIdentifier: cellName)

        }


        //控cell右边的箭头

        cell!.accessoryType =UITableViewCellAccessoryType.DisclosureIndicator

        if indexPath.row ==self.titleArr.count -1{

            cell!.accessoryType =UITableViewCellAccessoryType.None

        }

        

        //设置数据

        cell!.setData(self.titleArr.objectAtIndex(indexPath.row)as! String, row: indexPath.row)

        

        return cell!

    }

    

    //返回cell高度

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

        return 44.0;

    }

    

    //点击cell回调

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

        

        tableView.deselectRowAtIndexPath(indexPath, animated:true)

   }


   自定义cell的实现

   

    var titleLabel : UILabel?

    var phoneLabel : UILabel?

    var phoneImageView : UIImageView?

    

    

//重写init必写此方法

    required init?(coder aDecoder:NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    

    override init(style:UITableViewCellStyle, reuseIdentifier: String?) {

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        

        initTitleLabel();

        initPhoneLabel();

        initPhoneImageView();

    }

    

    func initTitleLabel(){

        

        self.titleLabel =UILabel();

        self.titleLabel?.backgroundColor =UIColor.clearColor();

        self.titleLabel?.textColor =UIColor(red: 100/255.0, green:100/255.0, blue:100/255.0, alpha:1.0);

        self.titleLabel?.font =UIFont.systemFontOfSize(14)

        self.contentView.addSubview(self.titleLabel!)

        

        //这是第三方约束(大家可以用自己的方式来实现控件位置的设置,frame也可以)

        self.titleLabel?.snp_makeConstraints(closure: { (make) ->Void in

            

            make.centerY.equalTo(self.contentView);

            make.left.equalTo(self.contentView).offset(15);

            make.width.equalTo(150);

            make.height.equalTo(12);

        })

    }

    //创建方式我就不写了

    func initPhoneLabel(){

        

        

    }

    

    //创建方式我就不写了

    func initPhoneImageView(){

        

        

    }

    

    //设置数据

    internal func setData(str :String,row : NSInteger){

        

        //最后一行

        if (row == 4) {

            self.phoneLabel!.hidden =false;

            self.phoneImageView!.hidden =false;

        }

        else{

            self.phoneLabel!.hidden =true;

            self.phoneImageView!.hidden =true;

        }

        //设置标题

        self.titleLabel!.text = str;

    }


   效果



0 0
原创粉丝点击