我一直在尝试为我的朋友为乐队创建我自己的应用程序,并且我一直在尝试使用自定义TableViewCell来查看网站上出现的新闻报道。我的主要目标是从网站获取所有数据,将其存储在NSMutableArray中,然后将其显示在我的自定义单元格中。UITableView在滚动时崩溃
当它加载第5-6个单元格时,应用程序运行良好。但是,当我开始滚动时,应用程序崩溃。我已经指出了cellForRowAtIndexPath:
方法。使用GDB,我也发现在创建我的NSMutableArray数据之后,一旦程序运行,滚动后,我的数组似乎就会自动释放。我不知道为什么会发生这种情况。下面是我对我的代码迄今:
在HomeViewController.h:
@interface HomeViewController : UIViewController {
NSArray *results;
NSMutableArray *titles;
NSMutableArray *dates;
NSMutableArray *entries;
}
@property (nonatomic, retain) NSMutableArray *titles;
@property (nonatomic, retain) NSMutableArray *dates;
@property (nonatomic, retain) NSMutableArray *entries;
@end
在HomeViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
titles = [[NSMutableArray alloc] init];
dates = [[NSMutableArray alloc] init];
entries = [[NSMutableArray alloc] init];
while((i+1) != endIndex){
NSString *curr_title = [[NSString alloc] init];
NSString *curr_date = [[NSString alloc] init];
NSString *curr_entry = [[NSString alloc] init];
//do some character iterations across a string
[titles addObject:curr_title];
[dates addObject:curr_date];
[entries addObject:curr_entry];
[curr_title release];
[curr_date release];
[curr_entry release];
}
}
//更多在这里的代码,删除
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"NewsCell";
NewsCell *cell = (NewsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle]
loadNibNamed:@"NewsCell"
owner:self options:nil];
// cell = [topLevelObjects objectAtIndex:0];
for(id currentObject in topLevelObjects){
if([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (NewsCell *)currentObject;
break;
}
}
}
NSLog(@"%d", indexPath.row);
NSLog(@"%d", titles.count);
cell.cellTitle.text = [titles objectAtIndex:indexPath.row];
cell.datePosted.text = [dates objectAtIndex:indexPath.row];
cell.preview.text = [entries objectAtIndex:indexPath.row];
return cell;
}
同样,前5-6个细胞出现。有一次,我滚动,我试图这样做po titles
和得到这个错误:
Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_PROTECTION_FAILURE at address: 0x00000009 0x011b309b in objc_msgSend()
我试过在initWithNibName:
分配数组,但似乎并没有做太多。我试过移动viewDidLoad:
中的所有代码,然后调用[super viewDidLoad]
,它也产生了相同的结果。对不起,这是很长的,但我想人们需要看我的代码。
感谢您的快速响应!我曾尝试使用NSZombiesEnabled,但它并没有提供足够奇怪的信息。我用过Instruments,它确实说过“有一个Objective-C消息被发送到地址为0x5e70870的释放对象(僵尸)。”它指出了一些自动释放的东西,所以我正在研究这一点。它似乎仍然像我的数组发布不知何故。 – SpacePyro
同时检查您添加到阵列的对象是否被过度释放。 – titaniumdecoy
我增加了一些代码(上面的编辑)。我不认为他们是过度发布的,但不知何故,他们是(我仍然试图获得Objective-c的悬挂!),我肯定会解决这个问题。 – SpacePyro