我有一个 UITableViewController 来管理使用子类原型单元创建的表格视图。最相关的代码如下:
MyCell.h
#import <UIKit/UIKit.h>
@interface ScrollViewTableCellInShorTrip : UITableViewCell
@end
我的细胞
#import "MyCell.h"
@implementation SMyCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
NSLog(@"touch cell");
[super touchesEnded: touches withEvent: event];
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
}
@end
表视图控制器.h
#import <UIKit/UIKit.h>
@interface TableViewController : UITableViewController{
}
@property (nonatomic, retain) NSArray *arr;
@end
表视图控制器.m
#import "TableViewController.h"
#import "MyCell.h"
@implementation ATripTableViewController
@synthesize arr;
- (void)viewDidLoad
{
[super viewDidLoad];
self.arr = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CellIdentifier = @"myCell";
MyCell *myCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return myCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"detailView"sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"detailView"]){
NSLog(@"arr acount is %d", self.arr.count);//program received signal: "EXC_BAD_ACCESS".
}
}
在 prepareForSegue:sender: 方法中调用 "NSLog(@"arr acount is %d", self.arr.count)" 时出现 EXC_BAD_ACCESS 错误消息。很明显,属性“arr”现在是免费的。只有当我使用子类 UITableViewCell 时才会出现这种情况。
感谢任何答案!