UIPickerView Scrolling Can cause crash with no error?

时间:2017-05-16 09:26:05

标签: ios objective-c uipickerview

Im using a picker view which seems to work but on occasion triggers a stack trace crash with no error for me to work with.

Here is my picker view setup, it works just fine, however at total random, it can crash the app.

Any ideas as to what could be the cause?

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
                return 1;
            }

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
                return _pickerChannels.count;
            }

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
                UILabel *labelSelected = (UILabel*)[_channelPickerView viewForRow:row forComponent:component];
                [labelSelected setTextColor:[UIColor whiteColor]];
                self.channelGroupValue = [_pickerChannels indexOfObject:_pickerChannels[row]];
                [[self delegate] sideBarDidScrollChannels:self.channelGroupValue];
                [[self delegate] sideBarDidSetScrollChannels:self.channelGroupValue];
            }

- (UIView *) pickerView: (UIPickerView *) pickerView viewForRow: (NSInteger) row forComponent: (NSInteger) component reusingView:(UIView *)view {
        UILabel *label = (id)view;
        if (!label)
        {
            label= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [_channelPickerView rowSizeForComponent:component].width,
                                                                         [_channelPickerView rowSizeForComponent:component].height)];
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = [UIColor whiteColor];
            [label setFont:[UIFont systemFontOfSize:17]];
            label.text = _pickerChannels[row];
        }
        return label;
    }

This is the only info i can provide on the crashing:

enter image description here

2 个答案:

答案 0 :(得分:0)

If your pickerChannels array is dynamic, I mean if it is changing it's value with other operations and if it become nil then it is possible to crash.

Second thing why you are doing,

  UILabel *label = (id)view;

if your view is label then you can do like,

  UILabel *label = (UILabel*)view;

viewForRow method returns UIView, so you should return UIView!! you can add label to one UIVIew and can return that view!

答案 1 :(得分:0)

I guess the crash is about CDCControl setTCPWrite which occasionally occurs because you are using multithread, as you can see on your stack.
To be more specified, maybe there are two threads calling CDCControl setTCPWrite at the same time.

相关问题