swift- 去除tableView 多余行

前端之家收集整理的这篇文章主要介绍了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
    }
}

猜你在找的Swift相关文章