在我的UITableViewCell中,我有一个名为initNotification的方法,它由TableViewController在cellForRowAtIndexPath中调用,其中创建TableCells。
我的问题是,每次重新加载此视图时,都会再次调用initNotification方法,因此当通知出现时,NotificationHandle将被调用x次!
我尝试使用以下代码在添加Observer之前将其删除:
但这也不起作用。问题是,我不能使用bool标志或类似的东西,因为单元格始终由ViewController重新初始化。
有没有适当的方法从通知中心删除NotificationHandle?
编辑:这是我创建自定义TableViewCells的方式。
我的问题是,每次重新加载此视图时,都会再次调用initNotification方法,因此当通知出现时,NotificationHandle将被调用x次!
我尝试使用以下代码在添加Observer之前将其删除:
-(void) initNotification{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(handleNotificationOnOff:)
name:[[NSString alloc] initWithFormat:@"%@",[self.light beckhoffOnOff]]
object:nil];
}
但这也不起作用。问题是,我不能使用bool标志或类似的东西,因为单元格始终由ViewController重新初始化。
有没有适当的方法从通知中心删除NotificationHandle?
编辑:这是我创建自定义TableViewCells的方式。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
Light* l = [[staticModel.model getRoomAtIndex:[indexPath section]]getLightAtIndex:[indexPath item]];
if([l typ]==ONOFF){
TableCellLight *conof = [tableView dequeueReusableCellWithIdentifier:@"ReuseIDOnOff" forIndexPath:indexPath];
LightOnOff *lonof = (LightOnOff*) l;
[[conof label] setText: [lonof bezeichnung]];
conof.light=lonof;
[conof initNotification];
cell = conof;
}
if([l typ]==DIMMER){
TableCellLightDim *cdim = [tableView dequeueReusableCellWithIdentifier:@"ReuseIDDim" forIndexPath:indexPath];
LightDim *ldim= (LightDim*) l;
[[cdim label] setText: [ldim bezeichnung]];
[[cdim slider]setValue:[ldim dimVal]];
cdim.light=ldim;
[cdim initNotification];
cell = cdim;
}
if([l typ]==RGB){
TableCellLightRGB *crgb = [tableView dequeueReusableCellWithIdentifier:@"ReuseIDRGB" forIndexPath:indexPath];
LightRGB *lrgb= (LightRGB*) l;
[[crgb label] setText: [lrgb bezeichnung]];
crgb.light=lrgb;
crgb.owner=self;
[crgb initNotification];
cell = crgb;
}
return cell;
}
谢谢
tableView:didEndDisplayingCell:forRowAtIndexPath:
来告诉每个单元格在从屏幕中移除时将自己作为观察者移除。 - Wain