我收到EXC_BAD_ACCESS,但我不明白为什么。 这是代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
TDBadgedCell *cell = [[[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
SubscriptionArray * element =[reader.subscriptions objectAtIndex:[indexPath row]];
[cell.textLabel setText:element.title]; // <--- error at this line of code
return cell;
}
reader.subscritions是NSMutableArray,包含81个元素(我确定用NSLog验证了)但是当我尝试设置标题时我有这个错误...为什么? SubscritionArray是一个自定义类,包含3个字符串和1个int。
自定义类: 头
#import <Foundation/Foundation.h>
@interface SubscriptionArray : NSObject{
NSString *title;
NSString *source;
NSString *htmlUrl;
NSInteger count;
}
@property (nonatomic,retain) NSString *title;
@property (nonatomic,retain) NSString *source;
@property (nonatomic,retain) NSString *htmlUrl;
@property (nonatomic) NSInteger count;
@end
实现:
#import "SubscriptionArray.h"
@implementation SubscriptionArray
@synthesize title,source,htmlUrl,count;
-(void)dealloc{
[title release];
[source release];
[htmlUrl release];
}
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
title = [[decoder decodeObjectForKey:@"title"] retain];
source = [[decoder decodeObjectForKey:@"source"] retain];
htmlUrl = [[decoder decodeObjectForKey:@"htmlUrl"] retain];
//count = [decoder decodeIntForKey:@"count"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
if (title) [encoder encodeObject:title forKey:@"title"];
if (source) [encoder encodeObject:source forKey:@"source"];
if (htmlUrl) [encoder encodeObject:htmlUrl forKey:@"htmlUrl"];
//if (count) [encoder encodeInteger:count forKey:@"count"];
}
@end
修改 的 我是我得到的 * - [__ NSArrayM objectAtIndex:]:发送到解除分配的实例0xe5a8260的消息
这就是我设置NSMutableArray的方式
subscriptions = [[NSMutableArray alloc]init];
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the
//documents directory:
NSString *fullFileName = [NSString stringWithFormat:@"%@/subscriptions", documentsDirectory];
[subscriptions removeAllObjects];
subscriptions = [NSKeyedUnarchiver unarchiveObjectWithFile:fullFileName];
答案 0 :(得分:0)
我冒昧地说你的订阅元素数组与你的行索引不匹配(只是一个猜测)。建议您在声明'element'之后和尝试设置标签之前立即设置调试器断点。您应该能够看到您是否实际获得了一个SubscriptionArray对象...
答案 1 :(得分:0)
reader.subscriptions
中的可变数组在您尝试在tableView:cellForRowAtIndexPath:
中引用之前被取消分配。确保阅读器正确保留或复制subscriptions
阵列,并且您不会通过在某处发送额外的release
或autorelease
消息来过度释放它。从reader
实例的类中发布相关代码可能会有所帮助。