swift3.0 自定义tableviewcell

来源:互联网 发布:软件离职项目交接 编辑:程序博客网 时间:2024/06/05 01:23

最近没事就打算看哈swift,先学习学一下tableview,简单总结了tableview一些的属性,有不好的地方不要介意,直接上代码吧

MainTableViewCell.swift

class MainTableViewCell:UITableViewCell {

    var titleLabel =UILabel()

    var imageview =UIImageView()

    overrideinit(style:UITableViewCellStyle, reuseIdentifier:String?) {

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

        if !self.isEqual(nil) {

            setUpViews()

       }

    }

    

    requiredinit?(coder aDecoder:NSCoder) {

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

    }

    

    func setUpViews()  {

        self.imageview.frame = CGRect(x:10,y:5,width:100,height:100)

        self.imageview.image=UIImage(named:"people")

        self.titleLabel.frame = CGRect(x:10,y:110,width:100,height:40)

        self.titleLabel.text = "领奖中心"

        self.titleLabel.font = UIFont.systemFont(ofSize:15)

        self.titleLabel.textColor = UIColor.black

        

        self.contentView .addSubview(self.imageview)

        self.contentView.addSubview(self.titleLabel)

        

    }


    overridefunc awakeFromNib() {

        super.awakeFromNib()

    }


    overridefunc setSelected(_ selected:Bool, animated:Bool) {

        super.setSelected(selected, animated: animated)

    }


}

MainViewController.swift


class MainViewController:UIViewController,UITableViewDelegate,UITableViewDataSource{

  

   staticlet identifier="identtifier";


    var label =UILabel()

    

    

    overridefunc viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor=UIColor.white

        self.view.addSubview(self.tableView)

        

    }

    publicfunc numberOfSections(in tableView:UITableView) ->Int {

        return8;

        

    }

    

    //行数

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

        return1;

        

    }

    publicfunc tableView(_ tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell

    {

        //初始化自定义cell

        var cell=tableView.dequeueReusableCell(withIdentifier:MainViewController.identifier)

        if(cell ==nil){

            cell=MainTableViewCell(style:UITableViewCellStyle.value1, reuseIdentifier:MainViewController.identifier);

        }

        return cell!

    }

    

    //定义tableview头部视图

    publicfunc tableView(_ tableView:UITableView, viewForHeaderInSection section:Int) ->UIView?

    {

        let view =UIView()

        view.backgroundColor=UIColor.lightGray

        let label =UILabel()

        label.frame=CGRect(x:10,y:10,width:100,height:20)

        label.text ="直播间"

        label.textColor=UIColor.white

        view.addSubview(label)

        return view;

        

    }

   //头部视图的高

    func tableView(_ tableView:UITableView, heightForHeaderInSection section:Int) ->CGFloat {

        return30;

        

    }

    lazyvar tableView :UITableView = {

        ()->UITableViewin

        let tableView =UITableView()

        tableView.backgroundColor =UIColor.white

        tableView.frame =CGRect(x:0, y:0, width:UIScreen.main.bounds.width, height:UIScreen.main.bounds.height)

        tableView.delegate =self

        tableView.dataSource =self

        tableView.rowHeight =170

        return tableView

    }()

     

}    






原创粉丝点击