I have an UITableViewController to manage an table view which created with subclassed prototype cells. The most related codes is as following:
MyCell.h
#import <UIKit/UIKit.h>
@interface ScrollViewTableCellInShorTrip : UITableViewCell
@end
MyCell.m
#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
TableViewController.h
#import <UIKit/UIKit.h>
@interface TableViewController : UITableViewController{
}
@property (nonatomic, retain) NSArray *arr;
@end
TableViewController.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".
}
}
EXC_BAD_ACCESS error message is appear when calls “NSLog(@”arr acount is %d”, self.arr.count)” in the prepareForSegue:sender: method. It is obviously that the property “arr” is free now. And The situation appears only when i use the subclassed UITableViewCell.
Appreciate any answers!
NSLog(@"arr acount is %d", self.arr.count);replace toNSLog(@"arr acount is %d",arr.count);definition of self:
self is a special variable which is a pointer to the object which received the message which invoked the currently executing method(!). In other words, it is the receiver of the message.
when you should call
self.objectrather than calling the object directly within an object.The difference between these two calls is that a call to self.object will make use of the accessors generated by the @synthesize directive. A call to the object directly will bypass these accessor methods and directly modify the instance variable