大神来解释一下为什么?记录一个tableview的问题

首先说明,我的这个问题不具有普遍性,是一个偶然才会发生的事件。
通常情况下,我们只需要把tableview的header和footer设置成0.01的高度就不会有问题了。如下图:


image.png

但是今天说的这种问题,却是不受tableview的header和footer的影响的。
问题描述:
1.tableview的type为grouped;
2.使用了viewModel,且viewModel是懒加载的;
3.使用多线程进行reloadData,且多线程中才第一次调用viewModel。

问题截图:


image.png
image.png

来看代码吧:
ViewController:

//
//  ViewController.swift
//  Test
//
//  Created by iOS on 2018/12/29.
//  Copyright © 2018 weiman. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!
    private lazy var viewModel = ViewModel(newTableView)
//    private var viewModel: ViewModel?
    
    private lazy var newTableView: UITableView = {
        return $0
    }( UITableView(frame: .zero, style: .grouped) )
    
    override func viewDidLoad() {
        super.viewDidLoad()
        automaticallyAdjustsScrollViewInsets = false
//        setup()
        loadData()
    }

    private func setup() {
//        let _ = viewModel
//        viewModel = ViewModel(tableView)
        
        let frame = CGRect(x: 0,
                           y: 100,
                           width: view.frame.size.width,
                           height: view.frame.size.height - 100 - 49)
        newTableView.frame = frame
        newTableView.backgroundColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)
        
    }

}

extension ViewController {
    
    private func loadData() {
        
//        let group = DispatchGroup()
//
//        group.enter()
//        let work1 = DispatchQueue(label: "1")
//        work1.async {
//            print("线程1")
//            Thread.sleep(forTimeInterval: 2)
//            print("线程1执行完成")
//            group.leave()
//        }
//
//        group.enter()
//        let work2 = DispatchQueue(label: "2")
//        work2.async {
//            print("线程2")
//            Thread.sleep(forTimeInterval: 3)
//            print("线程2执行完成")
//            group.leave()
//        }
//
//        group.notify(queue: .main) { [weak self] in
//            guard let self = self else { return }
//            let _ = self.viewModel
//            DispatchQueue.main.asyncAfter(deadline: .now() + 5.0, execute: {
//                print("reload")
//                self.viewModel.reload()
//            })
//        }
        
//        self.viewModel.reload()
        /*
         while(!isFinished) { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; }

         */
       
//        viewModel.tableView.delegate = viewModel
//        viewModel.tableView.dataSource = viewModel
        
        DispatchQueue.main.asyncAfter(deadline: .now()) {
            self.setup()
            self.view.addSubview(self.newTableView)
            self.newTableView.delegate = self.viewModel
            self.newTableView.dataSource = self.viewModel
            
            self.viewModel.reload()
        }
    }
}

ViewModel

//
//  ViewModel.swift
//  Test
//
//  Created by iOS on 2018/12/29.
//  Copyright © 2018 weiman. All rights reserved.
//

import UIKit

class ViewModel: NSObject {

    let tableView: UITableView
    
    init(_ tableView: UITableView) {
        self.tableView = tableView
        super.init()
        setup()
    }
    
    private func setup() {
        
//        tableView.delegate = self
//        tableView.dataSource = self
        
        tableView.estimatedRowHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.estimatedSectionHeaderHeight = 0
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        }
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.register(Header.self, forHeaderFooterViewReuseIdentifier: "header")
        tableView.register(Footer.self, forHeaderFooterViewReuseIdentifier: "footer")
    }
    
    func reload() {
        tableView.reloadData()
    }
}

extension ViewModel: UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:     return 1
        case 1:     return 3
        case 2:     return 1
        default:    return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = "哈哈哈哈"
        return cell
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header")
        header?.contentView.backgroundColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)
        return header
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footer  = tableView.dequeueReusableHeaderFooterView(withIdentifier: "footer")
        footer?.contentView.backgroundColor = #colorLiteral(red: 0.8549019694, green: 0.250980407, blue: 0.4784313738, alpha: 1)
        return footer
    }
}

extension ViewModel: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        print("heightForHeaderInSection: \(section)")
        switch section {
        case 1, 2: return 60
        default: return 0.01
        }
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        print("heightForFooterInSection: \(section)")
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(tableView.contentOffset)
    }
}

把代码修改成如下这样都是没有问题的:

方法一:把self.view.addSubview(self.newTableView)放在设置代理的后面。


image.png

方法二:去掉多线程

image.png

方法三:把设置代理的方法放在外面

image.png

方法四: 不使用懒加载;

方法五:在viewdidload中,先把懒加载给初始化;

实在想不明白为什么,还请大神出来解释一下呗,感激不尽。

demo地址:https://github.com/weiman152/TableViewGroupedTest/tree/master

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 223,277评论 6 521
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 95,487评论 3 400
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 170,114评论 0 366
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 60,367评论 1 300
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 69,352评论 6 398
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,919评论 1 314
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,328评论 3 426
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 40,278评论 0 277
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,808评论 1 321
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,882评论 3 343
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 41,005评论 1 354
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,672评论 5 351
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,344评论 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,839评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,959评论 1 275
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,481评论 3 379
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 46,031评论 2 361

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,113评论 1 32
  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 10,614评论 3 3
  • 面试题参考1 : 面试题[http://www.cocoachina.com/ios/20150803/12872...
    江河_ios阅读 1,748评论 0 4
  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 1,349评论 0 1
  • 醒来, 在清晨到来之前的2点57. 微风带着细雨, 骨子里思念像毒药在蔓延。 我确定,我想你。 你像那小巷里的孤灯...
    郑凌霄阅读 346评论 2 1