Expanded TableView Cells ошибка EXC_BAD_ACCESS

section
swift
tableviewcell
ios
tableview

#1

Приветствую.

Реализовал в своем проекте Expanded TableView Cells по уроку :
https://swiftbook.ru/post/tutorials/expanded-tableview-cells/

Но всегда рандомно вылетает ошибка EXC_BAD_ACCESS :

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
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
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
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

По коду все так как в уроке , шаг в шаг.

class InfoVC: UIViewController, UITableViewDelegate, UITableViewDataSource, HeaderViewDelegate {
   private static 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 {
        return 0
    }
    let countArray = ModelInfoArray.shared.modelArray[section].modelPodpunkt.count
    return countArray
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "InfoCell") as? InfoCell else {return UITableViewCell()}
    
        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: InfoVC.headerID) as? CustomHeaderView else {return nil}

    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([section], with: .automatic)
}
    
    private func tableViewConfig() {
        let nib = UINib(nibName: InfoVC.headerID, bundle: nil)
        tableView.register(nib, forHeaderFooterViewReuseIdentifier: InfoVC.headerID)
        tableView.tableFooterView = UIView()
    }
}

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
    }
    
    func rotateImage(_ expanded: Bool) {
        if expanded {
            UIView.animate(withDuration: 0.2) { [self] in
                headerButton.imageView?.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
            }
        } else {
            UIView.animate(withDuration: 0.2) { [self] in
                headerButton.imageView?.transform = CGAffineTransform(rotationAngle: CGFloat.zero)
            }
            
        }
    } 
}

Перебровал уже все, ни чего не помогает. Каждый раз ошибка в tableView.reloadSections([section], with: .automatic) и delegate?.expandedSection(button: sender)


#2

Решено путем замены reloadSections на reloadData, пожертвовав анимацией.
Но все равно интересно, почему возникала эта ошибка, у кого какие мнения?