美文网首页
Swift3.0 创建UITableView

Swift3.0 创建UITableView

作者: 望仔超甜 | 来源:发表于2017-06-23 12:17 被阅读20次

    效果图如下:


    Simulator Screen Shot 2017年6月23日 下午12.13.48.png

    用xib创建的cell , 请看如下代码:
    <pre>
    class HomePageViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    var tableView:UITableView!
    var dataArray = NSMutableArray()
    var imageArray = NSMutableArray()

    let cellID = "testCellID"
    var isEdit = false //判断tabview是否在编辑状态
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "首页"
        self.dataArray = ["BIGBANG","G-DRAGON","TOP","DAESUNG","SEUNGRI","TAEYANG"]
        self.imageArray = ["bigbang","gd","top","daesung","seungri","taeyang"]
    

    // self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "编辑", style: UIBarButtonItemStyle.done, target: self, action:#selector(HomePageViewController.rightBarButtonItemClicked))
    //初始化TableView
    self.tableView = UITableView(frame:self.view.frame,style:.plain)
    self.tableView.delegate = self
    self.tableView.dataSource = self

        //创建单元格
        self.tableView!.register(UINib(nibName:"XIBTableViewCell",bundle:nil), forCellReuseIdentifier: "XIBTableViewCell")
    

    // self.tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: cellID)

        self.view.addSubview(self.tableView)
    
        //去掉没有数据的cell的分割线
        self.tableView.tableFooterView = UIView()
    
    }
    
    //导航栏右侧按钮事件
    func rightBarButtonItemClicked(){
    
        if isEdit{
            self.tableView.setEditing(false, animated: true)
            isEdit = false
        }else{
            self.tableView.setEditing(true, animated: true)
            isEdit = true
    
        }
    }
    //cell内容的显示
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:XIBTableViewCell = tableView.dequeueReusableCell(withIdentifier: "XIBTableViewCell", for: indexPath) as! XIBTableViewCell
        cell.cellLabel!.text = self.dataArray[indexPath.row] as? String
        cell.cellImage?.image = UIImage(named:self.imageArray[indexPath.row] as! String)
        return cell
    
    
    }
    //返回的cell的行数
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    //单元格高度
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    

    }
    }
    </pre>

    相关文章

      网友评论

          本文标题:Swift3.0 创建UITableView

          本文链接:https://www.haomeiwen.com/subject/fdjqcxtx.html