使用uitable的cellForRowAtIndexPath方法里数据对象总被莫名其妙的释放~求高人指点

chenghaoorange 2012-11-07 12:27:29
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *myIdentifier=@"OnlineCell";
static BOOL nibsRegistered = NO;
if (!nibsRegistered) {
UINib *nib = [UINib nibWithNibName:@"OnlineCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:myIdentifier];
nibsRegistered = YES;
}
NSLog(@"*********3");
OnlineCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
NSUInteger row=[indexPath row];
NSMutableDictionary *classDic = [CLASSNAMES objectAtIndex:[indexPath section]];
NSString *classid = [classDic objectForKey:@"id"];
NSLog(@"classid:%@",classid);

if([classVideoDic count]>0 && classid!=nil){
NSLog(@"*********??");
NSMutableArray *array = [classVideoDic objectForKey:classid];
NSMutableDictionary *dic =[array objectAtIndex:row];
NSLog(@"*********4");
NSString *cellName = [dic objectForKey:@"videoname"];
NSString *pic = [dic objectForKey:@"videopicurl"];
NSLog(@"*********5");
// NSLog(@"%@",cellName);
[cell.onlineLabel setText:cellName];
NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:pic]];
UIImage *image = [[UIImage alloc]initWithData:imageData];
[cell.imageView setImage:image];
}

return cell;
}

以上是方法里绑定数据的代码,报的错误是[__NSDictionaryM count]: message sent to deallocated instance
classVideoDic = [NSMutableDictionary dictionaryWithCapacity:5];
for (int i=0; i<[classVideoArray count]; i++) {
NSMutableDictionary *dic =[classVideoArray objectAtIndex:i];
NSString *classId = [dic objectForKey:@"videoclassid"];
if([classVideoDic objectForKey:classId]==nil){
NSLog(@"isnil");
NSMutableArray *array = [NSMutableArray arrayWithObjects:dic, nil];
[classVideoDic setObject:array forKey:classId];
}else{
NSLog(@"notnil");
NSMutableArray *array = [classVideoDic objectForKey:classId];
[array addObject:dic];
[classVideoDic setObject:array forKey:classId];
}
};
self.dataItems = classVideoArray;
NSLog(@"cc==%d",[classVideoDic count]);
NSLog(@"bb==%d",[classVideoArray count]);

以上代码是声明并赋值给classVideoDic对象的相关代码,classVideoDic是.h文件里声明的一个属性
知道报的错误表示classVideoDic对象被销毁了,但我没有其他地方掉过他的release方法,并且开始打印出的[classVideoDic count]是有值的,但不知道为什么在给tableview绑定时就成被释放的对象了,求高人指点啊!
其中第二段代码是[request setDidFinishSelector:@selector(getClassFinished:)];所绑定的回调函数getClassFinished里的
...全文
218 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Snake_Jay 2013-01-07
  • 打赏
  • 举报
回复
成员变量可以使用alloc方法来初始化,或者说你现在的方法后面加一个retain或者改成属性调用
四分之一炷香 2013-01-06
  • 打赏
  • 举报
回复
classVideoDic创建得时候用得是autorelease方式,虽然是一个熟悉,如果没有明确得对象所属,缺省为成员变量classVideoDic;一个成员变量用autorelase方式,它得释放跟autoreleasepool 有很大关系,不是很保险,说不定什么时候会释放; 解决方法: 1.classVideoDic 属性声明成retain方式,self.classVideoDic = [NSMutableDictionary dictionaryWithCapacity:5]; 2.创建classVideoDic 之后,调用[classVideoDic retain];当然在delalloc时也不许掉release;否则内存泄露

29,029

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧