加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift- 去除tableView 多余行

发布时间:2020-12-14 01:33:27 所属栏目:百科 来源:网络整理
导读:import UIKitclass FourthViewController: UIViewController { @IBOutlet weak var tableView: UITableView! let cellName = "cell" let myTableViewCellIdentified = "MyTableViewCell" override func viewDidLoad() { super.viewDidLoad() prepareUI() //
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
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读