在自定义 UITableViewCell 中释放 ViewController

Deallocated ViewController inside custom UITableViewCell

我的代码结构是这样的:

UITableViewController(带一个或多个)->自定义UITableviewCell(添加视图)->UIViewController

现在,要将对 UIViewController 的操作通知给 UITableViewController,我有一个遵循之前解释的逆向流程的协议,但是,当我对 UIViewController 执行某些操作时,应用程序崩溃,因为我正在尝试访问已解除分配的实例...

我以一种肮脏的方式避免了 UIViewController 上的 IBAction 崩溃:将 UIViewController 中的 属性 设置为 self

我该如何解决这个漏洞?这是我的代码:

UITableViewController:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GameTableViewCell *cell;

    cell = [tableView dequeueReusableCellWithIdentifier: CellId];
    [cell configureWithGame: currentGame];

    cell.delegate = self;
    return cell;
}

自定义 TableViewCell:

-(void)configureWithGame:(Game *)game
{
    outcomeController = [[OutcomeViewController alloc] initWithGame:game];
    outcomeController.delegate = self;

    activeGame = game;

    //Adapting outcomeView
    CGRect frame = outcomeController.view.frame;
    frame.size = self.outcomeView.frame.size;
    outcomeController.view.frame = frame;
    [[self.outcomeView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.outcomeView addSubview:outcomeController.view];
}

OutcomeViewController 有一个 属性 @property (nonatomic, strong) id forceRetain; 并以这种方式设置在 -(void)viewDidLoad 中:

self.forceRetain = self;

这会导致一些泄漏,我想解决这个问题。

尝试在您的手机代码中将 outcomeController 属性 设置为 strong

此外,使用您发布的代码,每次滚动 UITableView 时都会分配 OutcomeViewController。这是您想要的行为吗?