0

我有一个自定义 UITableViewCell (ReservationsCell.h/ReservationsCell.m/ReservationsCell.xib),其中有一个按钮。在NIB文件中,标识符设置为reservationsCell,File's Owner设置为View Controller,自定义类设置为ReservationsCell。按钮的 TouchUpInside 事件链接到编写在 ViewController 文件中的 IBAction。

问题

当我单击按钮时,我得到一个 EXC_BAD_ACCESS-[NSObject performSelector:withObject:withObject:]: message sent to deallocated instance 0x398e7ff0

尝试

过去几个小时教我尝试 NSZombies,这告诉我错误是由我的tableView:cellForRowAtIndexPath方法引起的。

代码

视图控制器.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    .....    

    // register ReservationCell nib
    [self.reservationsTableView registerNib:[UINib nibWithNibName:@"ReservationsCell" bundle:nil] forCellReuseIdentifier:@"reservationsCell"];   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"reservationsCell";
    ReservationsCell *cell = (ReservationsCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    ......
}

根据 Zombies 的说法,引发错误的特定行似乎是dequeueReusableCellWithIdentifier.

请帮我。提前致谢!:)

4

2 回答 2

1

发生的事情是您UIButton的 s 在重用的实例中定义UITableViewCell,因此可能附加到UITableViewCell已经释放和释放的实例。

你应该做的是照顾IBActionsUITableViewCell本身(ReservationsCell在你的情况下)。这些触摸事件应该UIViewController通过委托转发给协调事物(如果您需要单元格索引,也将其传递)。

祝你好运!

于 2013-09-15T14:27:38.290 回答
0

不要在自定义UITableViewCell笔尖中设置 FilesOwner - 它会导致奇怪的问题,其中包括您看到的这个错误。为单元本身设置自定义类,连接您的 UI 插座,并让自定义UITableViewCell类中的代码完成工作(或将工作委托给它的所有者UITableView)。

于 2013-09-15T14:24:50.027 回答