我的cell大概显示类似新浪微博cell中那样的内容大体是这么设计的:1、一登陆首先将要显示的文字内容异步下载完毕保存在缓存,cell显示的时候从缓存中取文字信息2、然后每当显示一个cell的时候,就异步下载该cell中的用户头像并显示3、每当显示一个cell的时候,若该cell中有用户发布的图像,则另开辟一个线程异步下载图像并显示可是3、中下载的图像老是莫名其妙的出现在其他的cell中请问这是怎么回事?谢谢
解决方案 »
- 新手求教如何代码实现UIView,同时使得里面的控件能够显示正常
- flash在iphone 中如何 和原生应用混合使用???
- Round Rect Button 爲什麽不能設置 Set to Back
- iphone 连接远程数据
- 请问关于XCode的使用openal的问题。
- ios apns心跳抓包
- 请教一下,苹果原生有这玩意儿吗
- 在做app架构的时候选择服务器需要考虑哪些因素?
- swift oc 混编,oc 如何访问swift类中的方法
- iOS
- XCode4.5.2里报错"can not init class"
- ios 摄像头 没有类似安卓上startprivew和stopprivew的功能
如果cell是你自己定义的子类,所有cell的内容都是在这个类的内部生成的话,则把这些视图的Frame的高度设为0,然后在下次加载再重新设置高度就可以了
{
NSInteger nowRow = indexPath.row;
HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeCell" forIndexPath:indexPath]; //cell子类
//关联cell内部view
[cell linkWithTag];
//读取缓存数据
NSString *userFaceString = [[appDelegate.homeConArr objectAtIndex:nowRow] objectForKey:@"face"]; //头像
NSString *userName = [[appDelegate.homeConArr objectAtIndex:nowRow] objectForKey:@"uname"]; //用户名
NSString *userCon = [[appDelegate.homeConArr objectAtIndex:nowRow] objectForKey:@"content"]; //微博内容
NSInteger hasImg = [[[appDelegate.homeConArr objectAtIndex:nowRow] objectForKey:@"type"] intValue]; //是否有微博图像
//写入内容 名字/文章
cell.userName.text = userName;
cell.userCon.text = userCon;
//创建刷新头像线程 并加入队列启动
NSArray *faceURLArray = [faceDic allKeys];
BOOL hasFaceCache = [faceURLArray containsObject:userFaceString];
if (!hasFaceCache) { //若无缓存
[cell.userFace setImage:[UIImage imageNamed:@"Placeholder.png"]]; //站位头像
NSArray *dicKeyArr = [[NSArray alloc] initWithObjects:userFaceString, indexPath, nil];
NSInvocationOperation *faceLoadOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadFace:) object:dicKeyArr];
[threadQueue addOperation:faceLoadOperation];
} else {
[cell.userFace setImage:[faceDic objectForKey:userFaceString]];
}
//线程下载文章图像
if (hasImg) {
//小图地址
NSString *smaImgUrlStr = [[[appDelegate.homeConArr objectAtIndex:nowRow] objectForKey:@"type_data"] objectForKey:@"thumburl"];
NSURL *imgUrl = [NSURL URLWithString:smaImgUrlStr];
cell.conImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl]];
NSLog(@"%@", smaImgUrlStr);
} else {
//
}
}基本上就是这样重用的cell只有文章图像的出现发生了混乱其他如头像,文字并没有混乱,不知道是怎么回事?
HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeCell" forIndexPath:indexPath];这就是重用啊
我的UITableViewCell是在storyboard中定义的Identifier为HomeCell的cell,所以是不是可以说UITableViewCell已在storyboard中初始化了,另外我也单独做了一个叫HomeCell的UITableViewCell的子类用于关联storyboard中的HomeCell
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
if (cell == nil)
{
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
return cell;你可以把UITableViewCell换成你的自定义类HomeCell试试
是的,两种方法都可以,但是为什么在给某个cell内控件设置了tag以后,这个控件又在其他的cell内出现了?
因为重用啊。肯定是你没设置内容的单元格,你说的其他cell也肯定是后来滚动到可见范围内的。
我了个擦,就设置了一个tag也会导致重用啊?!难道我还得给其他cell也定义一个没有用的该控件?
重用是再所难免的,属性都设一遍就可以了。不要出现没设置的属性,你这个例子里else里边设为nil应该就可以了