微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift- 去除tableView 多余行

import UIKit

class FourthViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    
    let cellName = "cell"
    let myTableViewCellIdentified = "MyTableViewCell"
    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
        // Do any additional setup after loading the view.
    }

    // 准备UI
    private func prepareUI() {
    
        navigationItem.title = "我的"
        
        self.tableView.delegate = self
        self.tableView.dataSource = self
        
        // 注册
        tableView.registerClass(UITableViewCell.self,forCellReuseIdentifier: cellName)
        
        //注册 
        tableView.registerNib(UINib(nibName: myTableViewCellIdentified,bundle: nil),forCellReuseIdentifier: myTableViewCellIdentified)
        //去除tableView 多余行的方法 添加一个tableFooterView 后面多余行不再显示
        tableView.tableFooterView = UIView()
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application,you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension FourthViewController: UITableViewDelegate,UITableViewDataSource {
    
    func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 1
        } else if section == 1{
            return 3
        } else if section == 2 {
            return 5
        } else {
            // do nothing.
            return 0
        }
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 3
    }
    
    func tableView(tableView: UITableView,heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 44
        } else {
            return 50
        }
    }
    
    func tableView(tableView: UITableView,titleForHeaderInSection section: Int) -> String? {
        if section == 0 {
            return "第一组"
        } else if section == 1{
            return "第二组"
        } else {
            return "第三组"
        }
    }
    
    func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        
        let cell = tableView.dequeueReusableCellWithIdentifier(cellName,forIndexPath: indexPath) as UITableViewCell
        if indexPath.section == 0 {
        cell.textLabel?.text = "123"
        } else if indexPath.section == 1{
            cell.textLabel?.text = "test"
        } else {
            let cell = tableView.dequeueReusableCellWithIdentifier(myTableViewCellIdentified,forIndexPath: indexPath) as! MyTableViewCell
            cell.textLabel?.text = "work"

        }
        return cell
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐