xmartlabs / XLForm

XLForm is the most flexible and powerful iOS library to create dynamic table-view forms. Fully compatible with Swift & Obj-C.
MIT License
5.78k stars 953 forks source link

App crashes using custom rows with xib file in iOS 11.X - Swift #1038

Closed bagatte closed 5 years ago

bagatte commented 5 years ago

Applications using XLForm running on iOS 11.X are crashing when using custom rows. After downloading the examples from https://github.com/xmartlabs/XLForm/tree/master/Examples/Swift, and trying to test Custom Rows, I've realized that the crash happens at XLFormRowDescriptor.cellForFormController(::) at line 136.

bundle = [NSBundle bundleForClass:NSClassFromString(cellClass)];

In this case it happens because XLFormRatingCell.swift and XLFormWeekDaysCell.swift, which inherit from XLFormBaseCell use a xib file for the cell.

NOTE It doesn't happen when running the app in the simulator.


-(XLFormBaseCell *)cellForFormController:(XLFormViewController * __unused)formController
{
    if (!_cell){
        id cellClass = self.cellClass ?: [XLFormViewController cellClassesForRowDescriptorTypes][self.rowType];
        NSAssert(cellClass, @"Not defined XLFormRowDescriptorType: %@", self.rowType ?: @"");
        if ([cellClass isKindOfClass:[NSString class]]) {
            NSString *cellClassString = cellClass;
            NSString *cellResource = nil;
            NSBundle *bundle = nil;
            if ([cellClassString rangeOfString:@"/"].location != NSNotFound) {
                NSArray *components = [cellClassString componentsSeparatedByString:@"/"];
                cellResource = [components lastObject];
                NSString *folderName = [components firstObject];
                NSString *bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:folderName];
                bundle = [NSBundle bundleWithPath:bundlePath];
            } else {
                bundle = [NSBundle bundleForClass:NSClassFromString(cellClass)]; // IT CRASHES HERE
                cellResource = cellClassString;
            }
            NSParameterAssert(bundle != nil);
            NSParameterAssert(cellResource != nil);

            if ([bundle pathForResource:cellResource ofType:@"nib"]){
                _cell = [[bundle loadNibNamed:cellResource owner:nil options:nil] firstObject];
            }
        } else {
            _cell = [[cellClass alloc] initWithStyle:self.cellStyle reuseIdentifier:nil];
        }
        _cell.rowDescriptor = self;
        NSAssert([_cell isKindOfClass:[XLFormBaseCell class]], @"UITableViewCell must extend from XLFormBaseCell");
        [self configureCellAtCreationTime];
    }
    return _cell;
}
mats-claassen commented 5 years ago

@bagatte Which is the crash? Also, to confirm, this crash only happens in iOS 11, not iOS 12 nor 10?

julienpouget commented 5 years ago

I had it on an iPad running iOS 9.3.5

mats-claassen commented 5 years ago

Merged the PR. Thanks for contributing!