Iphone 如何将图像加载到UITableView中的选定行?

Iphone 如何将图像加载到UITableView中的选定行?,iphone,image,uitableview,Iphone,Image,Uitableview,我正在尝试将图像加载到tableview行中。这个场景是,我有两个数组 nameID = [NSString stringWithFormat:@"01",@"02",@"05",@"06",@"07",@"08",@"09",nil]; 及 我将nameID值加载到tableview中。现在我需要将图像加载到行中,如果indexPath.row值包含在testID中。我试图加载,第一次它工作,但当我滚动,图像加载到所有行我滚动。那么,我如何以适当的方式实现这一点呢? 这是我的密码: NSSt

我正在尝试将图像加载到tableview行中。这个场景是,我有两个数组

nameID = [NSString stringWithFormat:@"01",@"02",@"05",@"06",@"07",@"08",@"09",nil];

我将
nameID
值加载到tableview中。现在我需要将图像加载到行中,如果
indexPath.row
值包含在
testID
中。我试图加载,第一次它工作,但当我滚动,图像加载到所有行我滚动。那么,我如何以适当的方式实现这一点呢? 这是我的密码:

NSString *temp1 = [friendsID objectAtIndex:indexPath.row];
if ([alertArray containsObject:temp1]) {
    tickImg.image = [UIImage imageNamed:@"tick.png"];
    [cell.contentView addSubview:tickImg];
}
请分享你的想法。
谢谢。

希望这个片段能帮助你

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    //configure cell
    NSString *imageName = [NSString stringWithFormat:@"%02d",indexPath.row]; //convert row number to string with format ##
    if ([testId containsObject:imageName]) {
        UIImage *img = [UIImage imageNamed:imageName];
        cell.imageView.image = img;
    }
    cell.textLabel.text = [nameId objectAtIndex:indexPath.row];

    return cell;
}

请包含cellForRowAtIndexPath方法中的代码。我已经添加了第一次加载图像时获得的代码,但当我滚动图像时,它也会加载到其他行。我如何控制它?我在所选行上获得了图像,但问题是当我滚动时,图像会在滚动时添加到其他行中。我怎么才能停止呢?实际上数组中的值并不像我在问题中提到的那样。它可能会有所不同。@John在顶部,它看起来像是一个单元重用问题。当
testId
不包含图像时,应将
imageView
image
置零,即在执行
cell.imageView.image=nil的位置添加
else
子句谢谢你的回复,我已经在这里发布了相同的内容,我得到了回复,工作正常,但在第一次出现表格时,图像没有加载。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    //configure cell
    NSString *imageName = [NSString stringWithFormat:@"%02d",indexPath.row]; //convert row number to string with format ##
    if ([testId containsObject:imageName]) {
        UIImage *img = [UIImage imageNamed:imageName];
        cell.imageView.image = img;
    }
    cell.textLabel.text = [nameId objectAtIndex:indexPath.row];

    return cell;
}