-->

iOS - Getting a weird error: unrecognized selector

2019-06-28 06:25发布

问题:

Introduction

In my current application I have a UITableView which holds custom cell objects. The custom UIViewCellObjects are simply subclassed from the standard UITableViewCell class. The custom cells holds information about running background uploads, and updates them with things like percentage done and so on.

The custom cell objects listens to NSNotifications from upload processes running in the background, and when they get a relevant notification, they simply update their own view controls with the new information (such as upload percentage).

Now when an upload process is done, I re-order the array of active upload objects and reload the tableview like this:

-(void) uploadFinished: (NSNotification*)notification
{
    NSDictionary *userInfo = [notification userInfo];

NSNumber *uploadID = [userInfo valueForKey:@"uploadID"];

if (uploadID.integerValue == uploadActivity.uploadID)
{   
    [[ApplicationActivities getSharedActivities] markUploadAsFinished:uploadActivity];
    [parentTable reloadData];

    [self setUploadComplete];
}
}

Now this method takes place in the tableviewcell objects, and as you can see they call their owning UITableView to reload the data right after the array is sorted. The markUploadAsFinished method simply re-orders the array so any newly finished upload is put at the top, so it will appear this way in the UITableView.

The Problem

Now the problem I'm having is that when this method is called, I sometimes get the following error: 'NSInvalidArgumentException', reason: '-[CALayer tableView:numberOfRowsInSection:]: unrecognized selector sent to instance

I do not get it all the time, sometimes the entire process runs fine and finished uploads appear in the start of the UItableview, and at other seemingly random times it fails. I don't really have a clue what's going on here.

The custom cells are loaded from a .NIB file like this:

    UploadCell *cell = [activeUploadsTable dequeueReusableCellWithIdentifier:@"UploadProgressCell"];

if (cell == nil)
{
    [[NSBundle mainBundle] loadNibNamed:@"UploadCellView" owner:self options:nil];

    cell = customCell;
}

Is there anyone who might have a clue about what's going on here?

EDIT

First of all, I have tracked down this error to appear right at the line where: reloadData

is called inside of the custom cell objects.

Furthermore, it seems that the instance it sends methods to can change. I just got this error too:

'NSInvalidArgumentException', reason: '-[UIScrollViewPanGestureRecognizer tableView:numberOfRowsInSection:]: unrecognized selector sent to instance

I really have no idea what's going on here.

回答1:

'-[CALayer tableView:numberOfRowsInSection:]: unrecognized selector sent to instance

You've got a bad pointer. It looks like your table's data source is being released while the table still exists. The table doesn't retain its data source because that could create a retain cycle. If you don't take care to keep the data source around while the table is using it, the table will can up with a pointer to an object that no longer exists. In this case, it looks like a CALayer object is subsequently being created at the same address. When the table later sends its "data source" a message to get the number of rows, that message is delivered to the layer, which (obviously) doesn't have a -tableView:numberOfRowsInSection: method, and the error results.



回答2:

According to me you running the upload process method in background i think other thread than main thread. And according to my knowledge when you deal with UIKIT objects you have ruin on main thread.

But these problem not occur every time because some time you switch to other thread to main thread so its working fine and some time not. so that problem occur