7

私のUITableViewCellには、TableCellが作成されるcellForRowAtIndexPathのTableViewControllerによって呼び出されるメソッドinitNotificationがあります。

私の問題は、このビューがリロードされるたびに initNotification メソッドが再度呼び出されるため、通知が表示されると、NotificationHandle が x-times と呼ばれることです!

オブザーバーを再度追加する前に、次の方法で削除しようとしました。

-(void) initNotification{
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(handleNotificationOnOff:)
     name:[[NSString alloc] initWithFormat:@"%@",[self.light beckhoffOnOff]]
     object:nil];
}

しかし、これも機能しません。問題は、セルがViewControllerによって常に再初期化されるため、bool-flagなどを使用できないことです。

NotificationCenter から 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;
}

ありがとう

4

1 に答える 1