滚动时 UITableview 重复图像 [英] UITableview repeating image when scroll

查看:36
本文介绍了滚动时 UITableview 重复图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,这是我的代码:

I have a problem, here is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UIImageView *tickImg = [[UIImageView alloc]init];
    tickImg.frame = CGRectMake(260, 28, 30, 30);
    NSInteger rowNum;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    if(searching)
    {
        cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];
        [cell.imageView setImageWithURL:[NSURL URLWithString:[tempArr objectAtIndex:indexPath.row]]
                       placeholderImage:[UIImage imageNamed:@"defaultPerson.png"]];  

    }else 
    {
        for (int i=0; i<[alertArray count]; i++) {
            NSString *temp = [NSString stringWithFormat:@"%@",[alertArray objectAtIndex:i]];
            if ([[friendsID objectAtIndex:indexPath.row] isEqualToString:temp]) {
                rowNum = indexPath.row;
                NSLog(@"Row: %d",rowNum);
            }
        }

        if (rowNum == indexPath.row) {
            tickImg.image = [UIImage imageNamed:@"tick.png"];
            [cell.contentView addSubview:tickImg];
        }
        [cell.imageView setImageWithURL:[NSURL URLWithString:[friendsImage objectAtIndex:indexPath.row]]  
                       placeholderImage:[UIImage imageNamed:@"defaultPerson.png"]];
        cell.textLabel.text = [friendsName objectAtIndex:indexPath.row];

    }
    return cell;

}

这里我有两个数组,一个是friendsID,另一个是alertArray.我已将friendsID 加载到tableview.在alertArray中我有一些ID,当一行中的friendsID包含alertArray时,我需要在行中显示一个刻度线.我试过上面的代码但没有用.请分享您的想法.谢谢.

Here I have two arrays, one is friendsID and the other is alertArray. I have loaded the friendsID to the tableview. And in the alertArray I have some IDs, I need to show a tick mark in the row when friendsID in a row contains alertArray. I have tried with above code but not working. Please share your ideas. Thanks.

推荐答案

我做了一些补充,也删除了一些代码.我不知道它是否会完美运行,但我知道如果您阅读此代码,那么您肯定会知道哪里出了问题.

Hi I have made some addition as well as removed some code too. I don't know whether it will run perfectly but I know that if you read this code then you surely get the idea what is wrong.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

/* not needed here 
UIImageView *tickImg = [[UIImageView alloc]init];
tickImg.frame = CGRectMake(260, 28, 30, 30);
NSInteger rowNum;
*/
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

//Here is my code
//removing the tickImg if it is in the super view
UIView *vw = [cell.contentView viewWithTag:99];
if(vw != nil){
    [vw removeFromSuperview];
}
//code endds here

if(searching)
{
    cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];
    [cell.imageView setImageWithURL:[NSURL URLWithString:[tempArr objectAtIndex:indexPath.row]]
                   placeholderImage:[UIImage imageNamed:@"defaultPerson.png"]];  

}else 
{
    for (int i=0; i<[alertArray count]; i++) {
        NSString *temp = [NSString stringWithFormat:@"%@",[alertArray objectAtIndex:i]];
        if ([[friendsID objectAtIndex:indexPath.row] isEqualToString:temp]) {
            /* this is not needed
            rowNum = indexPath.row;
            NSLog(@"Row: %d",rowNum);
             */

            //Here is my code
            UIImageView *tickImg = [[UIImageView alloc]init];
            //just giving the tag so that I can get it back when cell is reused. other wise you can use the custom cell and have a reference for this image view
            tickImg.tag = 99;
            tickImg.frame = CGRectMake(260, 28, 30, 30);
            //end of code
            tickImg.image = [UIImage imageNamed:@"tick.png"];
            [cell.contentView addSubview:tickImg];
            [tickImg release]; //if you are not using ARC
            break;
            //end of code
        }
    }
    /* this is also not needed 
    if (rowNum == indexPath.row) {
        tickImg.image = [UIImage imageNamed:@"tick.png"];
        [cell.contentView addSubview:tickImg];
    }
     */
    [cell.imageView setImageWithURL:[NSURL URLWithString:[friendsImage objectAtIndex:indexPath.row]]  
                   placeholderImage:[UIImage imageNamed:@"defaultPerson.png"]];
    cell.textLabel.text = [friendsName objectAtIndex:indexPath.row];

}
return cell;

}

这篇关于滚动时 UITableview 重复图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆