先说一下问题,
有一个全局变量
我在.m文件的开头 定义NSMutableDictionary *imageData;
在viewDidLoad方法里 imageData = [NSMutableDictionary dictionary];下边是使用过程中的一个段,就是刷新显示表格内容// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * CellFlg = @"cell";
UITableViewCell * cell = [tableView 
  dequeueReusableCellWithIdentifier:CellFlg];
if (cell==nil) { 
cell = [[[ UITableViewCell alloc]
 initWithStyle:UITableViewCellStyleDefault 
 reuseIdentifier:CellFlg] autorelease];
}
NSString * cellValue = [[nearData objectAtIndex:indexPath.row] objectForKey:@"title"];
cell.textLabel.text = cellValue;
        //这里每当屏幕滚动,重新执行的时候 就在这里崩溃了
        NSArray *imgArr = [imageData objectForKey:indexPath];
if (imgArr==nil){
[self startDownLoadImage :indexPath.row forIndexPath:indexPath];
//单元格图标
UIImage *image = [UIImage imageNamed:@"apple.jpeg"];
cell.imageView.image = image;
return cell;
}
return cell;
}
上边我加注释的地方 每当,也没滚动,重新装载表格内容的时候,访问到这里程序就崩溃了。
也就是访问 imageData 变量的时候崩溃。
这个变量的赋值是在 上边访问的一个方法中-(void) startDownLoadImage:(NSInteger)index forIndexPath:(NSIndexPath *) indexPath{
//使用ASIHttpRequest的异步下载 
NSMutableDictionary *test = nearData;
NSString *imgUrl = [[[test objectAtIndex:index] objectForKey:@"user"] objectForKey:@"picture"];
NSArray *imgArr = [NSArray arrayWithObjects:imgUrl,nil];
        //此处对 imageData 赋值
        [imageData setObject:imgArr forKey:indexPath];
[t_imageData setObject:imgArr forKey:indexPath];
NSURL *url = [NSURL URLWithString:imgUrl];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
request.indexPathInTableView = indexPath;
[request setDownloadDestinationPath:[[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"3.jpg"]];
[request startAsynchronous];
//[imgArr release];
//显示等待图标 
//[m_pIndicator startAnimating];
}
上边注释部分就是对 imageData赋值的操作。
以上就是问题描述,小弟新手,麻烦大家给看看 谢谢了。

解决方案 »

  1.   

    自己顶。。 麻烦帮忙给看看。 很困惑,之所以使用NSMutableDictionary 这样的类型,是因为他支持 key-value 机制,不知道还有别的数据类型能支持吗。
      

  2.   

    加断点
    GDB 一下,查看 imageData  内容或内存
      

  3.   

    断点调试了。
    在访问这个变量的时候加的
    NSArray *imgArr = [imageData objectForKey:indexPath];结果到不了这里就崩溃了。。我通过NSLog输出能够确定,程序执行到这里崩溃的。谢谢你得回答
      

  4.   

    全局变量得自己保持住, imageData = [NSMutableDictionary dictionary];这样自动释放了,加个retain,或者使用init方法试试
      

  5.   


    我已经按你说的做了。去掉了imageData = [NSMutableDictionary dictionary];
    而且定义 imageData 为属性 @property (retain) NSMutableDictionary *imageData;还是不行。麻烦再看看
      

  6.   

    在Cocoa设计模式中,有一种模式叫:享元
    意思就是共享内存块,UITableView就是采用这种设计模式,比如说你的数据一共有40条,你在页面上显示的
    只有10条,那么另外30条实际上跟这已经显示的10条共用的内存块时同一个。你说在你滑动的时候才会Crash,那么滑动的时候TableView会把Cell的内存指针重新分配所以当你给出一个下载请求后,并且在没有下载完成时滑动cell,此时你需要回调的指针已经不存在了。具体你可以看看cocoa设计模式中“享元”那一章节另外在初始化图片的时候尽量不要使用imageNamed,这个方法会将图片加进缓存,除非程序终止,否则不会释放
      

  7.   

    http://115.com/file/dn1pqkh7#babyFound.zip
    我的代码,都头疼两天了,搞不好。帮忙给我看看。加载玩列表,再点击刷新后就崩溃谢谢
      

  8.   


    如果你定义了 @property (retain) 的话.
    在声明的时候写 self.imageData = [NSMutableDictionary dictionary];
      

  9.   

    你的代码分享到期了 看不到源码 是在下面那句崩溃?只好看看是不是下面问题。
    把 这个NSString * cellValue = [[nearData objectAtIndex:indexPath.row] objectForKey:@"title"];
      试试
    改成   NSUInteger row = [indexPath row];
          NSDictionary *rowData = [self.nearData objectAtIndex:row];
          NSString * cellValue = [rowData objectForKey:@"title";
         
          NSArray *imgArr = [imageData objectForKey:indexPath]; ??这里 imgArr是个数组?  
          
      

  10.   

     NSArray *imgArr = [imageData objectForKey:indexPath]; 需要retain以下,官方文档已经说明了。然后使用后,再release一次。