UITableview повторяющееся изображение при прокрутке - PullRequest
0 голосов
/ 22 ноября 2011

У меня проблема, вот мой код:

- (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 в таблицу. И в alertArray у меня есть несколько идентификаторов, мне нужно показать галочку в строке, когда friendsID в строке содержит alertArray. Я пытался с приведенным выше кодом, но не работает. Пожалуйста, поделитесь своими идеями. Спасибо.

1 Ответ

1 голос
/ 22 ноября 2011

Привет, я сделал некоторые дополнения, а также удалил код тоже. Я не знаю, будет ли он работать идеально, но я знаю, что если вы прочитаете этот код, вы наверняка поймете, что не так.

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

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...