Why does UITableviewCell UINib init crash

77 views Asked by At

I am using UITableviewCell in view using UINib and adding it to contentView.

I don't get any error when I debug or test from testflight but Crashlytics has a lot of crashes

0x102d20 std::__1::vector<unsigned char, geo::StdAllocator<unsigned char, 
ggl::Allocator> >::__append(unsigned long) + 208
1  VectorKit                      0x102ce0 std::__1::vector<unsigned char, 
geo::StdAllocator<unsigned char, ggl::Allocator> >::__append(unsigned long) + 144
2  VectorKit                      0x10b7cc ggl::BufferData::BufferData(unsigned long, 
ggl::BufferType, ggl::BufferChange, ggl::BufferUsage, unsigned long) + 196
3  VectorKit                      0x200e38 
6  VectorKit                      0x61eb8 md::MapEngine::updateForConfigurationAndMode() + 1376
7  VectorKit                      0xada9c md::MapEngine::MapEngine(float, float, float, bool, VKMapViewPurpose, std::__1::shared_ptr<md::TaskContext> const&, VKMapPurpose, std::__1::unique_ptr<md::AnimationManager, std::__1::default_delete<md::AnimationManager> >, geo::linear_map<md::MapEngineSetting, long long, std::__1::equal_to<md::MapEngineSetting>, std::__1::allocator<std::__1::pair<md::MapEngineSetting, long long> >, std::__1::vector<std::__1::pair<md::MapEngineSetting, long long>, std::__1::allocator<std::__1::pair<md::MapEngineSetting, long long> > > > const&, unsigned long long, GEOApplicationAuditToken*) + 13772
8  VectorKit                      0x1070d8 md::MapEngine::interactiveMapEngine(float, bool, VKMapViewPurpose, geo::linear_map<md::MapEngineSetting, long long, std::__1::equal_to<md::MapEngineSetting>, std::__1::allocator<std::__1::pair<md::MapEngineSetting, long long> >, std::__1::vector<std::__1::pair<md::MapEngineSetting, long long>, std::__1::allocator<std::__1::pair<md::MapEngineSetting, long long> > > > const&, unsigned long long, GEOApplicationAuditToken*) + 152
9  VectorKit                      0x2619fc -[VKMapView initShouldRasterize:inBackground:contentScale:auditToken:mapViewPurpose:allowsAntialiasing:] + 936
10 MapKit                         0x141570 -[MKBasicMapView initWithFrame:andGlobe:shouldRasterize:allowsAntialiasing:] + 388
11 MapKit                         0x75094 -[MKMapView _commonInitFromIB:gestureRecognizerHostView:locationManager:showsAttribution:showsAppleLogo:allowsAntialiasing:] + 1668
12 MapKit                         0x7606c -[MKMapView initWithCoder:] + 252
13 UIFoundation                   0x63588 UINibDecoderDecodeObjectForValue + 704
14 UIFoundation                   0x637b4 UINibDecoderDecodeObjectForValue + 1260
15 UIFoundation                   0x3a58 -[UINibDecoder decodeObjectForKey:] + 324
16 UIKitCore                      0x45cac8 -[UIView initWithCoder:] + 976
17 UIFoundation                   0x63588 UINibDecoderDecodeObjectForValue + 704
18 UIFoundation                   0x637b4 UINibDecoderDecodeObjectForValue + 1260
19 UIFoundation                   0x3a58 -[UINibDecoder decodeObjectForKey:] + 324
20 UIKitCore                      0x45cac8 -[UIView initWithCoder:] + 976
21 UIKitCore                      0x20310 -[UITableViewCellContentView initWithCoder:] + 48
22 UIFoundation                   0x63588 UINibDecoderDecodeObjectForValue + 704
23 UIFoundation                   0x637b4 UINibDecoderDecodeObjectForValue + 1260
24 UIFoundation                   0x3a58 -[UINibDecoder decodeObjectForKey:] + 324
25 UIKitCore                      0x45cac8 -[UIView initWithCoder:] + 976
26 UIKitCore                      0xddd54 -[UITableViewCell initWithCoder:] + 100
27                           0x20b8c8 init + 45 (LocationFieldTableViewCell.swift:45)
28                           0x20ad98 init + 4341656984 (<compiler-generated>:4341656984)
29 UIKitCore                      0x414d3c -[UIClassSwapper initWithCoder:] + 2120
30 UIFoundation                   0x63588 UINibDecoderDecodeObjectForValue + 704
31 UIFoundation                   0x3a58 -[UINibDecoder decodeObjectForKey:] + 324
32 UIKitCore                      0x592ba8 -[UIRuntimeConnection initWithCoder:] + 132
33 UIFoundation                   0x63588 UINibDecoderDecodeObjectForValue + 704
34 UIFoundation                   0x637b4 UINibDecoderDecodeObjectForValue + 1260
35 UIFoundation                   0x3a58 -[UINibDecoder decodeObjectForKey:] + 324
36 UIKitCore                      0x595468 -[NSCoder(UIIBDependencyInjectionInternal) _decodeObjectsWithSourceSegueTemplate:creator:sender:forKey:] + 492
37 UIKitCore                      0x5343e4 -[UINib instantiateWithOwner:options:] + 1052
38                           0x209168 instanceFromNib + 74 (LocationFieldTableViewCell.swift:74)
1

There are 1 answers

2
Andrew Carter On

Let the table view create the cells for you by registering the nibs with the table view. This lets the table view only create the number of instances needed to fill the screen. For example:

class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Assuming a nib exists named LocationFieldTableViewCell in the main bundle
        let nib = UINib(nibName: "LocationFieldTableViewCell", bundle: .main)
        
        tableView.register(nib, forCellReuseIdentifier: "my-id")
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "my-id", for: indexPath) as? LocationFieldTableViewCell
        
        // configure cell
        
        return cell
    }

}

Read up on UITableView and how cell reuse works!

As far as the crash goes, you can probably get more information by following this guide https://developer.apple.com/news/?id=nra79npr

This will let you browse the code and see where it's crashing, likely something in the nib not decoding correctly - looking at the log (LocationFieldTableViewCell.swift:74), what's on line 74 of LocationFieldTableViewCell.swift? That's likely where the issue is.