在awakeFromNib
中调用UITableViewCell
时添加了NSNotification观察者。然后,我在调用removeFromSuperView
时删除了观察者。
- (void)awakeFromNib
{
[super awakeFromNib];
[self setNotificationObserver];
_vHolder.layer.cornerRadius = 10.0f;
_vHolder.layer.shadowColor = [UIColor blackColor].CGColor;
_vHolder.layer.shadowRadius = 2.0f;
_vHolder.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
_vHolder.layer.shadowOpacity = 0.5f;
}
- (void)removeFromSuperview
{
[super removeFromSuperview];
[self removeNotificationObserver];
}
- (void)setNotificationObserver
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveESSMQTTMessageNotification:) name:NOTIF_ESSMQTT_MESSAGE_RECEIVED object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveDeviceStatesMessageNotification:) name:NOTIF_DEVICE_STATES_RECEIVED object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveDeviceOnOffStateNotification:) name:NOTIF_DEVICE_ON_OFF_STATE_RECEIVED object:nil];
}
- (void)removeNotificationObserver
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NOTIF_ESSMQTT_MESSAGE_RECEIVED object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NOTIF_DEVICE_STATES_RECEIVED object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NOTIF_DEVICE_ON_OFF_STATE_RECEIVED object:nil];
}
我正在使用NSNotification
刷新此UITableViewCell
中我的按钮和图片的状态。
我面临的问题是,每次收到NSNotication时,都会调用awakeFromNib
。这将导致我的按钮和图像的状态刷新回其初始状态。 奇怪的是,我之前从未见过removeFromSuperview
被调用。
所以我的问题是:
为什么在收到awakeFromNib
时会调用NSNotification
?
我只是想知道,当您希望awakeFromNib
观察UITableViewCells
时,在NSNotifications
下添加观察者是正确的做法吗? (好吧,我一直这样做。)
答案 0 :(得分:0)
因为您的单元格已刷新内容但未从superview中删除
当成功刷新单元格内容时,必须调用removeNotificationObserver。
我建议您使用协议而不是通知。
希望帮助。