1
我的NSMutableArray似乎被釋放,我無法揣摩出,這裏是我的代碼:爲什麼在我的UITableView解除分配的NSMutableArray
- (void)awakeFromNib {
arrayOfData = [[NSMutableArray alloc] initWithCapacity:0];
[arrayOfData addObject:@"test"];
NSLog(@"array: %@", arrayOfData);
}
一切都很好,在這裏!
然後我創建一個UITable並將其添加到視圖中,然後我請求一些數據,一切都正常。
arrayOfData = [response objectForKey:@"results"];
NSLog(@"count: %i", [arrayOfData count]);
NSLog(@"success!");
numberOfRows = [arrayOfData count];
[self.myTableView reloadData];
創建的行數也工作正常:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"test: %i", numberOfRows);
return numberOfRows;
}
現在訪問我的陣列獲得在該行一個「發送到釋放實例的消息」。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// crashes here!
NSLog(@"array: %i", [arrayOfData count]);
if (numberOfRows){
NSString *filename = [[arrayOfData objectAtIndex:indexPath.row] objectForKey:@"filename"];
NSString *url = [NSString stringWithFormat: @"http://www.web.com/dir/%@", filename];
[cell.imageView setImageWithURL:[NSURL URLWithString:url]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
cell.textLabel.text = @"My Text";
}
return cell;
}
我也有這個在我的頭,我用@synthesize以及
@property (nonatomic, retain) NSMutableArray *arrayOfData;
任何幫助,將不勝感激!
啊,是的,這是問題。我的問題是,如果我重新分配它,它爲什麼會釋放?當「響應」被釋放時它會釋放嗎? –
這是一種可能性。我不知道這些類是做什麼的,但也可能是[response objectForKey:@「results」]返回一個自動釋放對象。 – paulmelnikow
你做了我的一天。 :) 我現在只想知道,如果它更好地保留陣列或致電 'arrayOfData = [NSMutableArray initWithArray:[response objectForKey:@「results」]]]; – Maverick1st