我的cell大概显示类似新浪微博cell中那样的内容大体是这么设计的:1、一登陆首先将要显示的文字内容异步下载完毕保存在缓存,cell显示的时候从缓存中取文字信息2、然后每当显示一个cell的时候,就异步下载该cell中的用户头像并显示3、每当显示一个cell的时候,若该cell中有用户发布的图像,则另开辟一个线程异步下载图像并显示可是3、中下载的图像老是莫名其妙的出现在其他的cell中请问这是怎么回事?谢谢

解决方案 »

  1.   

    UITableViewCell是重用的,当然你也可以让它不重用,重用的时候,你前面加载图片的View或者加载文字的View还上面,需要先把子视图移除;
    如果cell是你自己定义的子类,所有cell的内容都是在这个类的内部生成的话,则把这些视图的Frame的高度设为0,然后在下次加载再重新设置高度就可以了
      

  2.   

    - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        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只有文章图像的出现发生了混乱其他如头像,文字并没有混乱,不知道是怎么回事?
      

  3.   


    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeCell" forIndexPath:indexPath];这就是重用啊
      

  4.   

    谢谢 vikiliu0310 看了你的讲解,我这么理解的不知道对不对?cell采用重用后,后一个cell内控件的内容都是前一个cell所设置的内容,要是想改变,必须重新设置新值?但是,我在给某个cell内控件设置tag了以后,这个控件又在其他的cell内出现了,不知道是什么情况?
      

  5.   

    请问你的HomeCell在哪里初始化的,还有你这句HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeCell" forIndexPath:indexPath];并不是复用,只是取出在内存中的cell,后面并没有采用复用
      

  6.   


    我的UITableViewCell是在storyboard中定义的Identifier为HomeCell的cell,所以是不是可以说UITableViewCell已在storyboard中初始化了,另外我也单独做了一个叫HomeCell的UITableViewCell的子类用于关联storyboard中的HomeCell
      

  7.   

    恩,了解,我基本都是用代码实现的,没有用到IB,storyboard。代码一般复用的写法就是
        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试试
      

  8.   


    是的,两种方法都可以,但是为什么在给某个cell内控件设置了tag以后,这个控件又在其他的cell内出现了?
      

  9.   

    >但是为什么在给某个cell内控件设置了tag以后,这个控件又在其他的cell内出现了?
    因为重用啊。肯定是你没设置内容的单元格,你说的其他cell也肯定是后来滚动到可见范围内的。
      

  10.   


    我了个擦,就设置了一个tag也会导致重用啊?!难道我还得给其他cell也定义一个没有用的该控件?
      

  11.   

    重用就是完全一样的东西(用同一块内存),当然包括设置的tag。
    重用是再所难免的,属性都设一遍就可以了。不要出现没设置的属性,你这个例子里else里边设为nil应该就可以了