[英]UITableView crashes when scrolling
我一直在嘗試為我的朋友創建一個樂隊的應用程序,我一直在嘗試使用自定義的TableViewCell來顯示網站上的新聞文章。 我的主要目標是從網站獲取所有數據,將其存儲在NSMutableArray中,然后在我的自定義單元格中顯示。
應用程序在加載前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:
分配數組initWithNibName:
但這似乎沒有做太多。 我試過在viewDidLoad:
移動所有代碼viewDidLoad:
然后調用[super viewDidLoad]
,它也產生了相同的結果。 對不起,這太長了,但我想人們需要看到我的代碼。
我發布的代碼沒有看到任何明顯錯誤。 嘗試啟用NSZombieEnabled環境變量 。 這可以防止對象被釋放,這樣當您的應用程序“崩潰”時,您可以確定哪個對象導致了問題。
此外,您應該將所需對象分配給類的IBOutlet
屬性,而不是循環遍歷loadNibNamed:owner:options
返回的對象數組。 有關示例,請參閱從Nib文件加載自定義表格查看單元格 。
以下是從您發布的新代碼中提取的:
NSString *curr_title = [[NSString alloc] init];
//do some character iterations across a string
[titles addObject:curr_title];
[curr_title release];
NSString不可變(與NSMutableString一樣)。 你是否故意在titles數組中添加空字符串?
響應您的注釋: stringByAppendingString
創建一個新的自動釋放的NSString對象。
NSString *curr_title = [[NSString alloc] init];
// this leaks the original NSString object (curr_title no longer points to it);
// curr_title now points to a new, autoreleased NSString
curr_title = [curr_title stringByAppendingString:@"..."];
[titles addObject:curr_title];
// releasing the autoreleased NSString will cause your application to crash!
[curr_title release];
問題可能出在NewsCell的實施上,所有屬性都被保留了?
另外,HomeViewController是UIViewController的子類而不是UITableViewController的任何原因?
這應該工作得很好:
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"NewsCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
* EXC_BAD_ACCESS *是一個確定的跡象,表明您的某個對象已經過度釋放(或未正確保留)。 NSZombieEnabled在這里是你的朋友,正如titaniumdecoy建議的那樣 - 弄清楚什么對象被釋放是一半的戰斗。 請務必在發布應用程序之前關閉它,因為(正如鈦指出的那樣)它可以防止對象被釋放。
我通常使用NSZombieEnabled和放置良好的斷點的組合(所以我可以遍歷代碼直到它崩潰)來找出問題在代碼中出現的位置。 然后通常只需回溯即可找出物體釋放的位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.