2015-07-10 28 views
2

這是我每次滾動我的標籤時都會出現的代碼文本越來越重疊。文本重疊,我認爲這是因爲重複使用細胞。 我正在使用故事板,這不是一個自定義單元格。 需要一些幫助,每一個幫助將不勝感激。我有原型表格視圖單元格的問題,我的單元格文本每次重疊時都會重疊

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

static NSString *TableViewCEll [email protected]"TableViewCell"; 
UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:TableViewCEll forIndexPath:indexPath]; 
if (Cell == nil) { 

    Cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCEll]; 
} 
UIImageView *imageView=[[UIImageView alloc]init]; 
imageView.frame =CGRectMake(5, 5, 100,110); 
[Cell.contentView addSubview:imageView]; 

NSArray * array =[[self.ListOfCarArray objectAtIndex:indexPath.row]objectForKey:@"CarImage"]; 

if (array.count >0) { 

    NSString * str = [NSString stringWithFormat:@"%@uploads/Car/%@",[self.GetUserConfiDict objectForKey:@"server"],[[array objectAtIndex:0]objectForKey:@"name"]]; 

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]init]; 
    manager.responseSerializer = [AFImageResponseSerializer serializer]; 
    [manager GET:str 
     parameters:nil 
     success:^(AFHTTPRequestOperation *operation, id responseObject) { 

      imageView.image = responseObject; 
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
      NSLog(@"Failed with error %@.",[error localizedFailureReason]); 
     }]; 

} 

UILabel * ModelLbl =[[UILabel alloc]initWithFrame:CGRectMake(120, 10, 120, 25)]; 
ModelLbl.textColor  = [UIColor whiteColor]; 
NSDictionary *str = [[NSDictionary alloc]init]; 
str =[[self.ListOfCarArray objectAtIndex:indexPath.row]objectForKey:@"Car"]; 
ModelLbl.text = [str objectForKey:@"model"]; 
[Cell.contentView addSubview:ModelLbl]; 


int x = 120; 
int y = 50; 
for (int i = 1; i<=5; i++) { 

    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 84,15)]; 
    label.textColor  = [UIColor whiteColor]; 

    if (i ==1) { 

     id null = [str objectForKey:@"year"]; 
     if ([null isKindOfClass:[NSNull class]]) { 
      label.text [email protected]""; 
     } 
     else{ 
      label.text = [str objectForKey:@"year"]; 
      [Cell.contentView addSubview:label]; 
     } 
    } 
    else if (i == 2){ 

     id null = [str objectForKey:@"color"]; 
     if ([null isKindOfClass:[NSNull class]]) { 
      label.text [email protected]""; 
     } 
     else{ 
      label.text =[str objectForKey:@"color"]; 
      [Cell.contentView addSubview:label]; 
     } 
    } 
    else if (i == 3){ 

     id null = [str objectForKey:@"price"]; 
     if ([null isKindOfClass:[NSNull class]]) { 
      label.text [email protected]""; 
     } 
     else{ 
      label.text =[str objectForKey:@"price"]; 
      [Cell.contentView addSubview:label]; 
      x+=100; 
      y = 30; 
     } 
    } 
    else if (i ==4) { 

     id null = [str objectForKey:@"miles"]; 
     if ([null isKindOfClass:[NSNull class]]) { 
      label.text [email protected]""; 
     } 
     else{ 
      label.text =[str objectForKey:@"miles"]; 
      [Cell.contentView addSubview:label]; 
     } 
    } 
    else if (i == 5){ 

     id null = [str objectForKey:@"stock_number"]; 
     if ([null isKindOfClass:[NSNull class]]) { 
      label.text [email protected]""; 
     } 
     else{ 
      label.text =[str objectForKey:@"stock_number"]; 
      [Cell.contentView addSubview:label]; 
     } 
    } 
    y+=20; 
} 

return Cell; 
} 

回答

1

我得到了我的答案,這就是我正在尋找..

for (UILabel *lbl in cell.contentView.subviews) 
{ 
    if ([lbl isKindOfClass:[UILabel class]]) 
    { 
     [lbl removeFromSuperview]; 
    } 
} 
0

是的,這是由表視圖電池的再利用而引起的。它不僅是標籤,還有imageView。您每一次這種方法

[Cell.contentView addSubview:imageView]; 

加入imageView,而在創建單元時,它應該只完成,即在

if (Cell == nil) { 
    Cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCEll]; 
} 

裏面我建議使用custom UITableViewCell subclass,但如果你堅持目前的做法,我會做這種方式:

static int IMAGE_VIEW_TAG = 1; 
if (Cell == nil) { 
    Cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCEll]; 
    UIImageView *imageView = [[UIImageView alloc]init]; 
    imageView.frame = CGRectMake(5, 5, 100,110); 
    imageView.tag = IMAGE_VIEW_TAG; 
    [Cell.contentView addSubview:imageView]; 
} 

而且下面,你可以通過標籤指的ImageView:

((UIImageView *)[Cell.contentView viewWithTag:IMAGE_VIEW_TAG]).image = responseObject; 

您必須對標籤執行相同操作。

相關問題