2011-11-22 93 views
0

我有一個問題,這裏是我的代碼:UITableView的重複圖像時滾動

- (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時,我需要在行中顯示一個刻度標記。我已經嘗試使用上面的代碼,但不工作。 請分享你的想法。 謝謝。

回答

1

嗨我已經做了一些補充以及刪除了一些代碼。 我不知道它是否會完美運行,但我知道如果你閱讀這段代碼,那麼你一定會明白什麼是錯的。

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

}

+0

非常感謝,這是一個真正偉大的,它是工作的罰款。唯一的問題是在表格加載的第一次,它沒有顯示圖像。我需要滾動表格才能查看圖像。爲什麼會這樣呢? – Mithuzz

+0

它可能是由於'if(searching){.......}'語句... –

+0

不,我沒有這種條件也試過,但沒有工作。 – Mithuzz