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
在自定义单元对象内部调用。
此外,它发送方法的实例似乎可以改变。我也刚刚收到此错误:
'NSInvalidArgumentException', reason: '-[UIScrollViewPanGestureRecognizer tableView:numberOfRowsInSection:]: unrecognized selector sent to instance
我真的不知道这里发生了什么。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你有一个错误的指针。看起来您的表的数据源正在被释放,而该表仍然存在。该表不保留其数据源,因为这可能会创建保留周期。如果在表使用数据源时不注意保留数据源,则表可能会出现一个指向不再存在的对象的指针。在这种情况下,看起来随后在同一地址创建了一个 CALayer 对象。当表稍后向其“数据源”发送一条消息以获取行数时,该消息将传递到该层,该层(显然)没有
-tableView:numberOfRowsInSection:
方法,以及错误结果。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.据我所知,您在后台运行上传过程方法,我认为除了主线程之外还有其他线程。据我所知,当你处理 UIKIT 对象时,你会毁掉主线程。
但这些问题并不是每次都会发生,因为有时您切换到其他线程到主线程,所以它工作正常,有时则不然。所以出现这个问题
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