i am working on a uitableview and i am getting random crashes with when i use custom uiviewcontroller inside a uitableviewcell.
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
SuspectSalesResponse* r = [valueArray objectAtIndex:indexPath.row];
SuspectSalesCellViewController* scellView = [[SuspectSalesCellViewController alloc]initWithNibName:@"SuspectSalesCellViewController" bundle:nil cellData:r];
if(![cellViewArray containsObject:scellView])
[cellViewArray addObject:scellView];
[cell.contentView addSubview:scellView.view];
}
return cell;
This code sometimes work, sometimes it gives the folowing error:
* -[SuspectSalesHeaderViewController _parentModalViewController]: message sent to deallocated instance 0x8556880
i’ve tried keeping contentviews within an array (cellViewArray) but no luck i am still getting this error. What am i doing wrong?
Thanks for helping!
The problem is that you are not supposed to add view controllers inside table cells. You should try to change from
UIViewControllertoUIView.The exact reason for your crash is that you create a controller (which has a view) and you are adding the view to your cell, but the controller object is lost. When the view sends whatever notification to it’s parent controller, the controller is already deallocated and there is where you get your bad access. This will be solved if you transform your
SuspectSales~to aUIViewsubclassEDIT
To load a
UIView(or any subclass) from a nib file, you must first create the xib, set the view’s class to your class (you do it in the third tab on the interface builder).After that, you can load it with this code:
Generic function in some utility class
In your class: