2011-01-25 43 views
9

Tôi có vấn đề lạ này với bàn của tôiUITableViewCell - chồng chéo với nội dung các tế bào trước

  1. Tôi có khoảng 20 tế bào để hiển thị
  2. Mỗi tế bào là khoảng 84px chiều cao
  3. Khi tôi bấm không tế bào , tôi đã đặt màu nền
  4. 4 ô đầu tiên là ok, nhưng khi tôi cuộn xuống và nhấp vào ô thứ 5, nội dung của mỗi ô bắt đầu chồng chéo với một số nội dung khác, thường là nội dung từ ô thứ nhất.

Tôi tin tưởng một số vấn đề về khả năng sử dụng lại hoặc vẽ của ô. Không chắc chắn làm thế nào để giải quyết nó, tôi đã kiểm tra thông qua mã của tôi, nhưng tôi không thay đổi nội dung của tế bào liên lạc.

Đây là mã của tôi và sẽ thêm một số bức ảnh quá Cell with overlapped content

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 104; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [stores count]; 
} 

-(UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    CGRect Label1Frame = CGRectMake(5, 5, 250, 30); 
    CGRect Label2Frame = CGRectMake(6, 42, 220, 20);  
    CGRect Label3Frame = CGRectMake(6, 62, 220, 20);   
    CGRect Label4Frame = CGRectMake(240,56, 70, 12);    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
    }else{ 
     // a cell is being recycled, remove the old edit field (if it contains one of our tagged edit fields) 
     UIView *viewToCheck = nil; 
     viewToCheck = [cell.contentView viewWithTag:1]; 
     if (!viewToCheck) { 
      [viewToCheck removeFromSuperview]; 
      DebugLog(@"View removed"); 
     }  
    } 
    //cell.selectionStyle=UITableViewCellSelectionStyleNone; 
    [cell setSelectedBackgroundView:bgView];  
    NSInteger row=indexPath.row; 
    UILabel *lblTemp; 
    [[cell contentView] clearsContextBeforeDrawing]; 

    //Line 1 

    lblTemp=[[UILabel alloc] initWithFrame: Label1Frame]; 
    lblTemp.tag=1; 
    lblTemp.text=[[stores objectAtIndex:row] objectAtIndex:0] ; 
    lblTemp.numberOfLines=2; 
    lblTemp.font = [UIFont boldSystemFontOfSize:13]; 
    lblTemp.adjustsFontSizeToFitWidth=YES; 
    lblTemp.minimumFontSize=12; 
    lblTemp.textColor = [UIColor grayColor]; 
    [cell.contentView addSubview:lblTemp]; 
    [lblTemp release]; 
    //Line 2 
    lblTemp = [[UILabel alloc] initWithFrame:Label2Frame]; 
    lblTemp.tag = 2; 
    lblTemp.text=[[stores objectAtIndex:row]objectAtIndex:1]; 
    lblTemp.font = [UIFont systemFontOfSize:12]; 
    lblTemp.textColor = [UIColor grayColor ]; 
    lblTemp.textAlignment=UITextAlignmentLeft; 
    lblTemp.adjustsFontSizeToFitWidth=YES; 
    lblTemp.minimumFontSize=12; 

    [cell.contentView addSubview:lblTemp]; 
    [lblTemp release]; 

    //Line 3  
    lblTemp = [[UILabel alloc] initWithFrame:Label3Frame]; 
    lblTemp.tag = 3; 
    lblTemp.text=[[stores objectAtIndex:row]objectAtIndex:2]; 
    lblTemp.font = [UIFont systemFontOfSize:12]; 
    lblTemp.textColor = [UIColor grayColor ]; 
    [cell.contentView addSubview:lblTemp];  
    [lblTemp release]; 
    //Phone button 
    UIButton *phoneButton=[[UIButton alloc] initWithFrame:CGRectMake(240,16,30,30)]; 
    [phoneButton setBackgroundImage:[UIImage imageNamed:@"phone.png"] forState:UIControlStateNormal]; 
    [phoneButton setTag:row]; 
    [phoneButton addTarget:self action:@selector(dialNumber:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.contentView addSubview:phoneButton]; 
    //ANnotation button 
    UIButton *annotation=[[UIButton alloc] initWithFrame:CGRectMake(274,16,30,30)]; 
    [annotation setTag:row]; 
    [annotation setBackgroundImage:[UIImage imageNamed:@"tab.png"] forState:UIControlStateNormal]; 
    [annotation addTarget:self action:@selector(openMap:) forControlEvents:UIControlEventTouchUpInside];  
    [cell.contentView addSubview:annotation]; 
    [annotation release]; 
    //Distance label 
    //Line 3  
    lblTemp = [[UILabel alloc] initWithFrame:Label4Frame]; 
    lblTemp.tag = 4; 
    lblTemp.text=[[stores objectAtIndex:row]objectAtIndex:5]; 
    lblTemp.textAlignment=UITextAlignmentCenter; 
    lblTemp.font = [UIFont systemFontOfSize:13]; 
    lblTemp.textColor = [UIColor grayColor ]; 
    [lblTemp setAdjustsFontSizeToFitWidth:YES]; 
    [cell.contentView addSubview:lblTemp];  
    [phoneButton release]; 
    [lblTemp release]; 
    [cell setNeedsLayout]; 
    [cell setNeedsDisplay]; 
    return cell; 
} 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath ]; 
    for(UILabel *lbl in cell.contentView.subviews){ 
     if([lbl isKindOfClass:[UILabel class]]){ 
      lbl.textColor=[UIColor whiteColor]; 

     } 
    } 
    //UITableViewCell *cell1;  
    //NSString *row=[NSString stringWithFormat:@"%d",indexPath.row]; 
    svm = [[storesMapView alloc] initWithNibName:@"storesMapView" bundle:nil]; 
    [svm initWithXML:stores:indexPath.row]; 
    CGRect theFrame = svm.view.frame; 
    theFrame.origin = CGPointMake(self.view.frame.size.width, 0); 
    svm.view.frame = theFrame; 
    theFrame.origin = CGPointMake(0,0); 
    theFrame.size=CGSizeMake(320,355); 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.3f]; 
    svm.view.frame = theFrame; 
    [UIView commitAnimations]; 
    [subView addSubview:svm.view]; 
    backButton.hidden=NO; 


} 
+0

Bạn thực sự nên xây dựng các tế bào như thế này trong một ngòi thay vì làm mọi thứ trong cellForRow. – Mark

+0

Bạn cũng có thể xem câu trả lời ở đây.Giải thích rõ ràng. http://stackoverflow.com/questions/8999300/labels-in-custom-cell-overlaps – user3675974

+0

tôi có cùng sự cố nhưng các bản xem trước của tôi không được khởi tạo bằng mã nhưng đến từ bảng phân cảnh. nếu xóa tất cả các bản phụ, tất cả đều đã biến mất. làm sao tôi có thể giải quyết việc này? – DamirDiz

Trả lời

19

I figured it out với một số thử và sai; nếu điều này có thể giúp một số một

Trong cellForRowAtIndexPath tôi đã cố gắng để xóa tất cả các tế bào subview trước khi rút ra các tế bào

//TRY TO REMOVE ALL CONTENT 
    for(UIView *view in cell.contentView.subviews){ 
     if ([view isKindOfClass:[UIView class]]) { 
      [view removeFromSuperview]; 
     } 
    } 

tôi sẽ rất vui nếu ai đó có thể chỉ cho tôi một số cách dễ dàng hơn

Cảm ơn

+0

Giải pháp tuyệt vời .... !! – TheTiger

+1

Cách dễ dàng hơn để thực hiện việc này là đặt tất cả các chế độ xem được thêm động vào một chế độ xem phụ phổ biến và xóa tất cả các xem phụ của chế độ xem này trong phương thức prepareForReuse của UITableViewCell –

+0

Đề xuất của NikolayKasyanov ở trên là địa điểm chính xác để thực hiện việc này. – Echelon

7

Bạn có thể sử dụng

[[[cell contentView] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)]; 

Nếu các tế bào trong không nil, các mã trên sẽ giảm thời gian cho việc sử dụng vòng lặp for tương tự như sau

for(UIView *eachView in [cell subviews]) 
    [eachView removeFromSuperview]; 
1

Tôi biết điều này là hơi muộn, nhưng tôi đã có một vấn đề tương tự nơi UILabels tạo ra cho một tế bào vẫn là một phần của tế bào khi nó được tái sử dụng. Vì vậy, mỗi cập nhật liên tiếp của tableview tạo ra một UILabel trên đầu trang của một hiện có. Tôi đã di chuyển việc tạo các Nhãn vào điều kiện if như dưới đây và nó đã giải quyết được vấn đề của tôi. Hy vọng nó sẽ giúp người khác. Cũng lưu ý không có bản phát hành nào như tôi đang sử dụng ARC.

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 

    { 

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    cityText = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)]; 
    cityText.font = [UIFont fontWithName:@"Arial" size:20]; 
    cityText.textAlignment = UITextAlignmentLeft; 
    cityText.backgroundColor = [UIColor clearColor]; 

    regionText = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 100, 20)]; 
    regionText.font = [UIFont fontWithName:@"Arial" size:20]; 
    regionText.textAlignment = UITextAlignmentLeft; 
    regionText.backgroundColor = [UIColor clearColor]; 

    } 
5

Tôi cũng có cùng issue.I cũng đã sáng tác tableView với labels.And khi tôi di chuyển nó xuống các nội dung đã overlapped.But nó đã giải quyết đơn giản bằng cách chỉnh sửa một tuyên bố trong cellForRowAtIndexPath.

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; 

Tôi nghĩ điều này sẽ giải quyết được vấn đề của bạn.

+0

Mã này làm việc cho tôi. – anjaly

0

Thiết nil nhãn văn bản trên UITableViewCell lớp con 's phương pháp prepareForReuse() giải quyết vấn đề của tôi -

override func prepareForReuse() { 
    super.prepareForReuse() 
    self.label1.text = nil 
    self.label2.text = nil 
    .... 
} 

chung nếu nó giúp bất cứ ai!

Các vấn đề liên quan