I have a custom UITableViewCell where I have a UIPickerView inside it. To manage that, I've created a Subclass where I implemented the UIPickerView delegate and datasource methods. When the cellForRowAtIndexPath I implemented like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section==2) {
PickerCellTableViewCell *cell2=[tableView dequeueReusableCellWithIdentifier:@"pickerCell" forIndexPath:indexPath];
cell2.cellPickerInputArray=self.pickerArray;
return cell2;
}else{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"normalCell" forIndexPath:indexPath];
cell.textLabel.text=[[self.inputArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
return cell;
}
}
on the subclass .m file I have the following:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [self.cellPickerInputArray count];
}
I have the following problem: If I leave it like this, it crashes and the console gives me this:
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (7) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
However, if I change the numberOfRowsInComponent: to return the actual number of rows (7 in this example), everything works pretty good.
I've been trying but I don't see to find the problem/solution. Any help would be appreciated. Thanks in advance!
Edit! As sugested by @meda I NSLoged NSLog(@"PickerInputArray count"%@",[self.cellPickerInputArray
count]); inside the method pickerView numberOfRowsInComponent
Here:
2014-03-30 21:04:54.756 TestPickerOnTable[3498:60b] PickerInputArray count0
2014-03-30 21:04:54.757 TestPickerOnTable[3498:60b] PickerInputArray count0
2014-03-30 21:04:54.758 TestPickerOnTable[3498:60b] PickerInputArray count0
2014-03-30 21:04:54.758 TestPickerOnTable[3498:60b] PickerInputArray count0
2014-03-30 21:04:54.762 TestPickerOnTable[3498:60b] PickerInputArray count7
2014-03-30 21:04:54.765 TestPickerOnTable[3498:60b] PickerInputArray count7
2014-03-30 21:04:54.767 TestPickerOnTable[3498:60b] PickerInputArray count7
2014-03-30 21:04:54.770 TestPickerOnTable[3498:60b] PickerInputArray count7
2014-03-30 21:04:54.771 TestPickerOnTable[3498:60b] PickerInputArray count7
2014-03-30 21:04:54.771 TestPickerOnTable[3498:60b] PickerInputArray count7
From your logs, it seems that the number of elements in your UIPicker
data source changes.
When you add or delete an element from self.inputArray
always make sure you reload the components or you will get the invalid update error.
to reload the picker:
[_yourPicker reloadAllComponents]