English 中文(简体)
滚动时可重复图像
原标题:UITableview repeating image when scroll

我的问题是我的法典:

- (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;

}

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;

}

问题回答

暂无回答




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签