我有一个自定义 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/