0

我有一个 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 时才会出现这种情况。

感谢任何答案!

4

2 回答 2

3

NSLog(@"arr acount is %d", self.arr.count);替换为NSLog(@"arr acount is %d",arr.count);

self 的定义:
self 是一个特殊变量,它是指向接收到调用当前正在执行的方法(!)的消息的对象的指针。换句话说,它是消息的接收者。

当您应该调用 self.object而不是直接在对象内调用对象时。

self.object = obj;
object = obj;

这两个调用之间的区别在于对 self.object 的调用将使用 @synthesize 指令生成的访问器。直接调用对象会绕过这些访问器方法,直接修改实例变量

于 2012-05-10T04:36:05.140 回答
2

count 是一个原始(整数),而不是一个对象,因为您将其称为 %@。使用 %i 代替。

于 2012-05-09T18:45:45.303 回答