【问题标题】:Where to addObserver to NSNotificationcenter in a custom UITableViewCell?在自定义 UITableViewCell 中将观察者添加到 NSNotificationcenter 的位置?
【发布时间】:2013-07-12 06:25:30
【问题描述】:

在我的 UITableViewCell 中,我有一个方法 initNotification,它由创建 TableCells 的 cellForRowAtIndexPath 中的 TableViewController 调用。

我的问题是,每次重新加载这个视图时,都会再次调用initNotification方法,所以当Notification出现时,NotificationHandle会被调用x次!

我尝试在再次添加之前删除观察者:

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

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

但这也不起作用。 问题是,我不能使用布尔标志或类似的东西,因为单元总是由 ViewController 重新初始化。

有没有合适的方法从 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;
}

谢谢

【问题讨论】:

    标签: ios uitableview nsnotificationcenter nsnotification


    【解决方案1】:

    一般来说,细胞不应该观察任何东西。控制器应该观察变化并将更新的信息推送到单元格上。

    在添加观察者之前调用removeObserver: 应该可以工作。如果您打算在prepareForReusetableView:didEndDisplayingCell:forRowAtIndexPath: 中执行任何操作来重置单元格,那将是您使用的代码。你需要看看你是如何测试它不工作的,以及你是如何重复使用细胞的。

    【讨论】:

    • 感谢您的回复!我已经弄清楚为什么 removeObserver: 不起作用,因为 self 不再相同,每次重新加载 TableView(以及单元格)时,我都会得到每个单元格的新实例。有没有办法强制单元格解除分配?
    • 如果您不重复使用它们应该被释放的单元格(例如,您没有重复使用标识符)。但这并不理想。
    • 我有一个重用标识符,但为什么要在同一个单元格的这么多实例上结束呢?
    • 尝试使用tableView:didEndDisplayingCell:forRowAtIndexPath: 告诉每个单元格在从屏幕上移除时将其作为观察者移除。
    • @Wain 我正在使用带有表格视图的 VC,其中一些单元格包含一个集合视图,这些单元格包含与需要观察的模型相关的内容。从视图类的最后一点观察模型对我来说非常有意义,毕竟他们最清楚他们关心什么样的数据。尽管如此,这次我实际上只在 VC 中进行了观察 - VC 仅刷新可见单元格。我相信 iOS 单元毕竟是相当棘手的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多