iOS-收到奇怪的错误:无法识别的选择器已发送到UITableView上的实例 [英] iOS - Getting a weird error: unrecognized selector sent to instance on a UITableView

查看:144
本文介绍了iOS-收到奇怪的错误:无法识别的选择器已发送到UITableView上的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介



在我当前的应用程序中,我有一个UITableView来保存自定义单元格对象。自定义UIViewCellObjects只是从标准UITableViewCell类的子类。自定义单元格包含有关正在运行的后台上传的信息,并使用完成百分比等内容对其进行更新。



自定义单元格对象从运行在后台的上传过程中监听NSNotifications。背景,当他们收到相关通知时,他们只需使用新信息(例如上载百分比)来更新自己的视图控件。



现在完成上载过程,我对活动的上载对象数组进行了重新排序,并像这样重新加载了tableview:

 -(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];
}
}

现在,此方法在tableviewcell对象中进行,并且如您所见,他们在数组排序后立即调用自己的UITableView重新加载数据。 markUploadAsFinished 方法只是简单地对数组重新排序,因此所有新完成的上载都放在顶部,因此它将以这种方式出现在UITableView中。



问题



现在我遇到的问题是,调用此方法时,有时 收到以下错误:
'NSInvalidArgumentException',原因:'-[CALayer tableView:numberOfRowsInSection:]:无法识别的选择器已发送到实例



我不是一直都得到它,有时整个过程运行良好,并且完成的上载会出现在UItableview的开头,而在其他看似随机的时间,它会失败。我真的不知道这是怎么回事。



自定义单元格是从.NIB文件加载的,如下所示:

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

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

cell = customCell;
}

有没有人对这里发生的事情有所了解?



编辑



首先,我已经找到了此错误,并显示在在以下行中:
reloadData



在自定义单元格对象内部被调用。



此外,它向其发送方法的实例似乎可以更改。我也刚收到这个错误:例如

我真的不知道这里发生了什么。

解决方案


'-[CALayer tableView:numberOfRowsInSection:]:无法识别的选择器已发送至实例


您的指针不好。看起来表格的数据源正在释放,而表格仍然存在。该表不会保留其数据源,因为这可能会创建一个保留周期。如果您在使用表时不注意保留数据源,则该表将带有指向不再存在的对象的指针。在这种情况下,看起来好像随后在同一地址创建了一个CALayer对象。当表稍后向其数据源发送消息以获取行数时,该消息将传递到图层(显然)没有 -tableView:numberOfRowsInSection:方法和错误结果。


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.

解决方案

'-[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.

这篇关于iOS-收到奇怪的错误:无法识别的选择器已发送到UITableView上的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆