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

TableView reloadSections 中的 EXC_BAD_ACCESS (SWIFT)

如何解决TableView reloadSections 中的 EXC_BAD_ACCESS (SWIFT)

Swift 中的扩展 TableView 单元格存在问题。 每次使用时,随机出现一个错误——EXC_BAD_ACCESS when执行reloadSections

完整日志:

0x10c7726e0 <+0>:   pushq  %rbp
0x10c7726e1 <+1>:   movq   %rsp,%rbp
0x10c7726e4 <+4>:   pushq  %r13
0x10c7726e6 <+6>:   subq   $0x158,%rsp              ; imm = 0x158 
0x10c7726ed <+13>:  movq   $0x0,-0x10(%rbp)
0x10c7726f5 <+21>:  movq   $0x0,-0x18(%rbp)
0x10c7726fd <+29>:  movq   $0x0,-0x20(%rbp)
0x10c772705 <+37>:  movb   $0x0,-0x48(%rbp)
0x10c772709 <+41>:  xorl   %eax,%eax
0x10c77270b <+43>:  movl   %eax,%ecx
0x10c77270d <+45>:  movq   %rdi,-0x90(%rbp)
0x10c772714 <+52>:  movq   %rcx,%rdi
0x10c772717 <+55>:  movq   %r13,-0x98(%rbp)
0x10c77271e <+62>:  callq  0x10ce1744c               ; symbol stub for: type Metadata accessor for Foundation.IndexSet
0x10c772723 <+67>:  movq   %rax,%rcx
0x10c772726 <+70>:  movq   -0x8(%rax),%rax
0x10c77272a <+74>:  movq   0x40(%rax),%rsi
0x10c77272e <+78>:  addq   $0xf,%rsi
0x10c772732 <+82>:  andq   $-0x10,%rsi
0x10c772736 <+86>:  movq   %rsp,%rdi
0x10c772739 <+89>:  subq   %rsi,%rdi
0x10c77273c <+92>:  movq   %rdi,%rsp
0x10c77273f <+95>:  movq   -0x90(%rbp),%rsi
0x10c772746 <+102>: movq   %rsi,-0x10(%rbp)
0x10c77274a <+106>: movq   -0x98(%rbp),%r8
0x10c772751 <+113>: movq   %r8,-0x18(%rbp)
0x10c772755 <+117>: movq   %rdi,-0xa0(%rbp)
0x10c77275c <+124>: movq   %rsi,%rdi
0x10c77275f <+127>: movq   %rdx,-0xa8(%rbp)
0x10c772766 <+134>: movq   %rcx,-0xb0(%rbp)
0x10c77276d <+141>: movq   %rax,-0xb8(%rbp)
0x10c772774 <+148>: callq  *0x8e93d6(%rip)           ; (void *)0x00007fff2018fa80: objc_retain
0x10c77277a <+154>: movq   -0x90(%rbp),%rcx
0x10c772781 <+161>: movq   0xa35370(%rip),%rsi      ; "tag"
0x10c772788 <+168>: movq   %rcx,%rdx
0x10c77278b <+171>: movq   %rdx,%rdi
0x10c77278e <+174>: movq   %rax,-0xc0(%rbp)
0x10c772795 <+181>: movq   %rcx,-0xc8(%rbp)
0x10c77279c <+188>: callq  0x10ce18c94               ; symbol stub for: objc_msgSend
0x10c7727a1 <+193>: movq   %rax,-0x20(%rbp)
0x10c7727a5 <+197>: movq   -0xc8(%rbp),%rcx
0x10c7727ac <+204>: movq   %rcx,%rdi
0x10c7727af <+207>: movq   %rax,-0xd0(%rbp)
0x10c7727b6 <+214>: callq  *0x8e938c(%rip)           ; (void *)0x00007fff2018f760: objc_release
0x10c7727bc <+220>: callq  0x10c6fec40               ; Picnic_Moscow.ModelInfoArray.shared.unsafeMutableAddressor : Picnic_Moscow.ModelInfoArray at ModelInfo.swift
0x10c7727c1 <+225>: movq   (%rax),%rax
0x10c7727c4 <+228>: movq   %rax,%rcx
0x10c7727c7 <+231>: movq   %rcx,%rdi
0x10c7727ca <+234>: movq   %rax,-0xd8(%rbp)
0x10c7727d1 <+241>: callq  0x10ce18fee               ; symbol stub for: swift_retain
0x10c7727d6 <+246>: movq   -0xd8(%rbp),%rcx
0x10c7727dd <+253>: movq   (%rcx),%rdx
0x10c7727e0 <+256>: movq   %rcx,%r13
0x10c7727e3 <+259>: movq   %rax,-0xe0(%rbp)
0x10c7727ea <+266>: callq  *0x60(%rdx)
0x10c7727ed <+269>: leaq   0x8e9dac(%rip),%rdx      ; type Metadata for Picnic_Moscow.ModelInfo
0x10c7727f4 <+276>: leaq   -0x40(%rbp),%rcx
0x10c7727f8 <+280>: movq   %rax,-0xe8(%rbp)
0x10c7727ff <+287>: movq   %rcx,%rax
0x10c772802 <+290>: movq   -0xd0(%rbp),%rdi
0x10c772809 <+297>: movq   -0xe8(%rbp),%rsi
0x10c772810 <+304>: callq  0x10ce17662               ; symbol stub for: Swift.Array.subscript.getter : (Swift.Int) -> A
0x10c772815 <+309>: movb   -0x40(%rbp),%r9b
0x10c772819 <+313>: leaq   -0x40(%rbp),%rdi
0x10c77281d <+317>: movb   %r9b,-0xe9(%rbp)
0x10c772824 <+324>: callq  0x10c772d90               ; outlined destroy of Picnic_Moscow.ModelInfo at <compiler-generated>
0x10c772829 <+329>: movb   -0xe9(%rbp),%r9b
0x10c772830 <+336>: andb   $0x1,%r9b
0x10c772834 <+340>: movb   %r9b,-0x48(%rbp)
0x10c772838 <+344>: movq   -0xd8(%rbp),%rdi
0x10c77283f <+351>: movq   %rax,-0xf8(%rbp)
0x10c772846 <+358>: callq  0x10ce18fe8               ; symbol stub for: swift_release
0x10c77284b <+363>: movq   -0xe8(%rbp),%rdi
0x10c772852 <+370>: callq  0x10ce18f5e               ; symbol stub for:    swift_bridgeObjectRelease
0x10c772857 <+375>: callq  0x10c6fec40               ;    Picnic_Moscow.ModelInfoArray.shared.unsafeMutableAddressor : Picnic_Moscow.ModelInfoArray at ModelInfo.swift
0x10c77285c <+380>: movq   (%rax),%rax
0x10c77285f <+383>: movq   %rax,%rcx
0x10c772862 <+386>: movq   %rcx,%rdi
0x10c772865 <+389>: movq   %rax,-0x100(%rbp)
0x10c77286c <+396>: callq  0x10ce18fee               ; symbol stub for: swift_retain
0x10c772871 <+401>: leaq   -0x68(%rbp),%rcx
0x10c772875 <+405>: movb   -0xe9(%rbp),%r9b
0x10c77287c <+412>: xorb   $-0x1,%r9b
0x10c772880 <+416>: movq   -0x100(%rbp),%rdx
0x10c772887 <+423>: movq   (%rdx),%rsi
0x10c77288a <+426>: movq   0x70(%rsi),%rsi
0x10c77288e <+430>: movq   %rcx,%rdi
0x10c772891 <+433>: movq   %rdx,%r13
0x10c772894 <+436>: movq   %rax,-0x108(%rbp)
0x10c77289b <+443>: movq   %rcx,-0x110(%rbp)
0x10c7728a2 <+450>: movb   %r9b,-0x111(%rbp)
0x10c7728a9 <+457>: callq  *%rsi
0x10c7728ab <+459>: leaq   -0x88(%rbp),%rcx
0x10c7728b2 <+466>: leaq   0xa7afdf(%rip),%rdi      ; demangling cache variable for type   Metadata for Swift.Array<Picnic_Moscow.ModelInfo>
0x10c7728b9 <+473>: movq   %rax,-0x120(%rbp)
0x10c7728c0 <+480>: movq   %rdx,-0x128(%rbp)
0x10c7728c7 <+487>: movq   %rcx,-0x130(%rbp)
0x10c7728ce <+494>: callq  0x10c6f49f0               ;    __swift_instantiateConcreteTypeFromMangledname at <compiler-generated>
0x10c7728d3 <+499>: movq   -0x130(%rbp),%rdi
0x10c7728da <+506>: movq   -0xd0(%rbp),%rsi
0x10c7728e1 <+513>: movq   %rax,%rdx
0x10c7728e4 <+516>: movq   -0x128(%rbp),%r13
0x10c7728eb <+523>: callq  0x10ce1765c               ; symbol stub for:    Swift.Array.subscript.modify : (Swift.Int) -> A
0x10c7728f0 <+528>: xorl   %esi,%esi
0x10c7728f2 <+530>: movb   -0x111(%rbp),%r9b
0x10c7728f9 <+537>: andb   $0x1,%r9b
0x10c7728fd <+541>: movb   %r9b,(%rdx)
0x10c772900 <+544>: movq   -0x130(%rbp),%rdi
0x10c772907 <+551>: callq  *%rax
0x10c772909 <+553>: xorl   %esi,%esi
0x10c77290b <+555>: movq   -0x110(%rbp),%rdi
0x10c772912 <+562>: movq   -0x120(%rbp),%rax
0x10c772919 <+569>: callq  *%rax
0x10c77291b <+571>: movq   -0x100(%rbp),%rdi
0x10c772922 <+578>: callq  0x10ce18fe8               ; symbol stub for: swift_release
0x10c772927 <+583>: movq   0x8e922a(%rip),%rax      ; (void *)0x00007fff873ea250:    swift_isaMask
0x10c77292e <+590>: movq   -0x98(%rbp),%rcx
0x10c772935 <+597>: movq   (%rcx),%rdx
0x10c772938 <+600>: andq   (%rax),%rdx
0x10c77293b <+603>: movq   %rcx,%r13
0x10c77293e <+606>: callq  *0x68(%rdx)
0x10c772941 <+609>: cmpq   $0x0,%rax
0x10c772945 <+613>: movq   %rax,-0x138(%rbp)
0x10c77294c <+620>: je     0x10c77295e               ; <+638> at InfoVC.swift:83:9
0x10c77294e <+622>: movq   -0x138(%rbp),%rax
0x10c772955 <+629>: movq   %rax,-0x140(%rbp)
0x10c77295c <+636>: jmp    0x10c7729cb               ; <+747> at <compiler-generated>
0x10c77295e <+638>: jmp    0x10c772960               ; <+640> at InfoVC.swift:83:9
0x10c772960 <+640>: jmp    0x10c772962               ; <+642> at InfoVC.swift:83:9
0x10c772962 <+642>: jmp    0x10c772964               ; <+644> at InfoVC.swift:83:9
0x10c772964 <+644>: jmp    0x10c772966               ; <+646> at InfoVC.swift:83:9
0x10c772966 <+646>: subq   $0x30,%rsp
0x10c77296a <+650>: leaq   0x7570ff(%rip),%rax      ; "Picnic_Moscow/InfoVC.swift"
0x10c772971 <+657>: movq   %rsp,%rcx
0x10c772974 <+660>: movq   %rax,(%rcx)
0x10c772977 <+663>: movl   $0x0,0x20(%rcx)
0x10c77297e <+670>: movq   $0x53,0x18(%rcx)
0x10c772986 <+678>: movl   $0x2,0x10(%rcx)
0x10c77298d <+685>: movq   $0x1a,0x8(%rcx)
0x10c772995 <+693>: leaq   0x75323e(%rip),%rdi      ; "Fatal error"
0x10c77299c <+700>: leaq   0x7522fd(%rip),%rcx      ; "Unexpectedly found nil while    implicitly unwrapping an Optional value"
0x10c7729a3 <+707>: movl   $0xb,%esi
0x10c7729a8 <+712>: movl   $0x44,%r8d
0x10c7729ae <+718>: movl   $0x2,%edx
0x10c7729b3 <+723>: movl   %edx,-0x144(%rbp)
0x10c7729b9 <+729>: movl   -0x144(%rbp),%r9d
0x10c7729c0 <+736>: callq  0x10ce177c4               ; symbol stub for:   Swift._assertionFailure(_: Swift.StaticString,_: Swift.StaticString,file: Swift.StaticString,line: Swift.UInt,flags: Swift.UInt32) -> Swift.Never
0x10c7729c5 <+741>: addq   $0x30,%rsp
0x10c7729c9 <+745>: ud2    
0x10c7729cb <+747>: movq   -0x140(%rbp),%rax
0x10c7729d2 <+754>: movq   -0xa0(%rbp),%rcx
0x10c7729d9 <+761>: movq   %rax,-0x150(%rbp)
0x10c7729e0 <+768>: movq   %rcx,%rax
0x10c7729e3 <+771>: movq   -0xd0(%rbp),%rdi
0x10c7729ea <+778>: callq  0x10ce17446               ; symbol stub for:   Foundation.IndexSet.init(integer: Swift.Int) -> Foundation.IndexSet
0x10c7729ef <+783>: movq   -0xa0(%rbp),%r13
0x10c7729f6 <+790>: callq  0x10ce17440               ; symbol stub for:   Foundation.IndexSet._bridgetoObjectiveC() -> __C.NSIndexSet
0x10c7729fb <+795>: movq   -0xb8(%rbp),%rcx
0x10c772a02 <+802>: movq   0x8(%rcx),%rdx
0x10c772a06 <+806>: movq   -0xa0(%rbp),%rdi
0x10c772a0d <+813>: movq   -0xb0(%rbp),%rsi
0x10c772a14 <+820>: movq   %rax,-0x158(%rbp)
0x10c772a1b <+827>: callq  *%rdx
0x10c772a1d <+829>: movq   0xa350dc(%rip),%rsi      ; "reloadSections:withRowAnimation:"
0x10c772a24 <+836>: movl   $0x64,%ecx
0x10c772a29 <+841>: movq   -0x150(%rbp),%rdi
0x10c772a30 <+848>: movq   -0x158(%rbp),%rdx
0x10c772a37 <+855>: callq  0x10ce18c94               ; symbol stub for: objc_msgSend
->  0x10c772a3c <+860>: movq   0x8e9105(%rip),%rax      ; (void *)0x00007fff2018f760: objc_release ---- ERROR: Thread 1: EXC_BAD_ACCESS (code=1,address=0xfffffffffffffff8)
0x10c772a43 <+867>: movq   -0x158(%rbp),%rdi
0x10c772a4a <+874>: movq   %rax,-0x160(%rbp)
0x10c772a51 <+881>: callq  *%rax
0x10c772a53 <+883>: movq   -0x150(%rbp),%rdi
0x10c772a5a <+890>: movq   -0x160(%rbp),%rax
0x10c772a61 <+897>: callq  *%rax
0x10c772a63 <+899>: leaq   -0x8(%rbp),%rsp
0x10c772a67 <+903>: popq   %r13
0x10c772a69 <+905>: popq   %rbp
0x10c772a6a <+906>: retq   

数据模型是从 plist 文件填充并存储在数组中的结构。该部分本身是从 Nib 文件加载的。

我不明白为什么,我检查了所有代码

class InfoVC: UIViewController,UITableViewDelegate,UITableViewDataSource,HeaderViewDelegate {
   
        private let headerID = String(describing: CustomHeaderView.self)
    
    @IBAction func cancelButton(_ sender: UIButton) {
        dismiss(animated: true) {
            ModelInfoArray.shared.removeItem()
        }
    }
    @IBOutlet weak var tableView: UITableView!
    

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        tableViewConfig()
        overrideUserInterfaceStyle = .light
        ModelInfoArray.shared.loadInfoVC()
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return ModelInfoArray.shared.modelArray.count
    }
    
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        if ModelInfoArray.shared.modelArray[section].isExpanded == false {
            return 0
        }
        guard let countArray = ModelInfoArray.shared.modelArray[section].modelPodpunkt?.count else {return 0}
        return countArray 
    }
    
    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "InfoCell") as! InfoCell
        
        cell.textInfoName.text = ModelInfoArray.shared.modelArray[indexPath.section].modelPodpunkt![indexPath.row].discription
        cell.imageLabel.image = ModelInfoArray.shared.modelArray[indexPath.section].modelPodpunkt![indexPath.row].image

        return cell
    }
    
    func tableView(_ tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? {
        
        guard let header = tableView.dequeueReusableheaderfooterView(withIdentifier: headerID) as? CustomHeaderView else {return UIView()}
        
        header.configure(title: ModelInfoArray.shared.modelArray[section].title,section: section)
        header.rotateImage(ModelInfoArray.shared.modelArray[section].isExpanded)
        header.delegate = self
                
        var backgroundConfig = UIBackgroundConfiguration.listPlainheaderfooter()
        backgroundConfig.backgroundColor = .white
        header.backgroundConfiguration = backgroundConfig
        
        return header
    }
    
    func tableView(_ tableView: UITableView,heightForHeaderInSection section: Int) -> CGFloat {
        return 80
    }

    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        scrollView.alwaysBounceHorizontal = false
        scrollView.alwaysBounceVertical = true
    }
    
    func expandedSection(button: UIButton) {
        let section = button.tag
        let isExpanded = ModelInfoArray.shared.modelArray[section].isExpanded
        ModelInfoArray.shared.modelArray[section].isExpanded = !isExpanded
        tableView.reloadSections(IndexSet(integer: section),with: .automatic)
    }
        
        private func tableViewConfig() {
            let nib = UINib(nibName: headerID,bundle: nil)
            tableView.register(nib,forheaderfooterViewReuseIdentifier: headerID)
            tableView.tableFooterView = UIView()

        }
    }

安装 button.tag:

protocol HeaderViewDelegate: class {
func expandedSection(button: UIButton)
}
class CustomHeaderView: UITableViewheaderfooterView {

    weak var delegate: HeaderViewDelegate?
    
    @IBOutlet weak var textInfo: UILabel!
    @IBOutlet weak var headerButton: UIButton!
    @IBAction func headerButton(_ sender: UIButton) {
        delegate?.expandedSection(button: sender)
    }
    
    func configure(title: String,section: Int) {
            textInfo.text = title
            headerButton.tag = section
        }
}

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