ios - 单击自定义 UITableViewCell 上的按钮会抛出 EXC_BAD_ACCESS

标签 ios objective-c uitableview exc-bad-access

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

问题

当我单击按钮时,我收到一条 EXC_BAD_ACCESS -[NSObject PerformSelector:withObject:withObject:]: 发送到已释放实例 0x398e7ff0 的消息

尝试

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

代码

ViewController.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

请帮帮我。提前致谢! :)

最佳答案

发生的情况是,您的 UIButton 是在重用的 UITableViewCell 实例中定义的,因此可能会附加到已发布的 UITableViewCell 实例并解除分配。

您应该做的是处理 UITableViewCell 本身中的 IBAction(在您的情况下为 ReservationsCell)。这些触摸事件应该转发到通过委托(delegate)协调事物的 UIViewController(如果您需要单元格索引,也将其传递)。

祝你好运!

关于ios - 单击自定义 UITableViewCell 上的按钮会抛出 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18813318/

相关文章:

ios - 在同一个 UITableView 的不同部分显示多个 NSFetchedResultsControllers

uitableview - 如何将 UILabels 正确插入到每个 tebleview 单元格中

ios - 当应用程序转到后台时,Google 移动广告 SDK 显示的广告会消失

ios - 检测 iOS 版本的最佳位置(方法)在哪里?

ios - 滚动到 Collection View 中的项目会使应用程序崩溃

iphone - SimpleAudioEngine 不播放

objective-c - 仅显示 UITableView 中的前 10 个单元格

ios - Swift 网络调用 : Latency keeps increasing and finally request timeout

iphone - 在 iOS 中自动创建图形(条形图和饼图)并将其转换为 pdf,而无需为图形创建 uiview

c# - 如何计算多边形的圆角?