2011-02-17 4 views
1

필자는 몇 주 동안이 문제를 겪었습니다.(맞춤) 스크롤 후 UITableViewCell이 뒤섞입니다.

기본적으로 내가/아래의 TableView의 타르 내에서 스크롤 할 때 사용자의 모든 콘텐츠가 혼합됩니다 IB 설계 셀 및

필자는 여러 솔루션을 시도했지만 아무 소용이, 당신거야 내 코드를 변명이 잘못 사용 조금.

사람들은 테이블 셀에 대한 subView를 계속 제안하지만 iOS 개발에 아직 전혀 익숙하지 않은 방법이 있습니다. 가능한 대답이 있다면 가능하면 자세하게 설명해주십시오. 다시 한번

, 미안 내 코드 =/

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

    static NSString *MyIdentifier = @"MyIdentifier"; 
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    NSInteger intCellTag; 

    NSDictionary *dictionary = [[[self.tableDataSource objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row]; 


    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease]; 


     [[[NSBundle mainBundle] loadNibNamed:@"EventsCustomTVCell" owner:self options:nil] lastObject]; 
     cell = tvCell; 
     self.tvCell = nil; 


     cell.textLabel.backgroundColor = [UIColor clearColor]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

     cell.tag = intCellTag; 


     intCellTag++; 

     UIImage *customCellBG = [UIImage imageNamed:@"EventsCustomTableCellBG.png"]; 
     UIImageView *customCellBGImageView = [[UIImageView alloc] initWithImage: customCellBG]; 
     customCellBGImageView.contentMode = UIViewContentModeScaleToFill; 
     cell.backgroundView = customCellBGImageView; 
     [customCellBGImageView release]; 




     [cell.contentView addSubview:imgThumbnail]; 
     [cell.contentView addSubview:lblName]; 
     [cell.contentView addSubview:lblDescription]; 
     [cell.contentView addSubview:lblDate]; 

    } 



    imgThumbnail.image = [UIImage imageNamed:[dictionary objectForKey: @"Thumbnail"]]; 
    lblName.text = [dictionary objectForKey:@"Title"]; 
    lblDescription.text = [dictionary objectForKey:@"Description"]; 
    lblDate.text = [dictionary objectForKey:@"Date"]; 


    return cell; 


} 

답변

1

.xib에서로드하고 수동으로 하위 뷰를 만드는 각각의 UITableViewCell을 정의 할 때 은유를 혼합하는 것처럼 보입니다. 물론이 문제 아무것도하지만,이 같은 직접 tableviewCell에 이미지와 레이블을 넣을 수 없었다 :

enter image description here

을 여기에 각 행을 표시하는 코드입니다 (당연히 IB에서 당신은 비 할당 한 @ "% d 개"indexPath를 각 제로 고유의 태그를 사용하면

있는 NSString * cellIdentifier = [있는 NSString stringWithFormat을 사용할 수 있습니다) 당 행으로

#define kImageTag 1 
#define kLabel1Tag 2 
#define kLabel2Tag 3 
#define kLabel3Tag 4 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"MyTvCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     self.tvCell = nil; 
     [[NSBundle mainBundle] loadNibNamed:@"TvCell" owner:self options:nil]; 
     cell = self.tvCell; 
    } 

    UIImageView *iv = (UIImageView *) [cell viewWithTag:kImageTag]; 
    UILabel *lbl1 = (UILabel *) [cell viewWithTag:kLabel1Tag]; 
    UILabel *lbl2 = (UILabel *) [cell viewWithTag:kLabel2Tag]; 
    UILabel *lbl3 = (UILabel *) [cell viewWithTag:kLabel3Tag]; 

    iv.image = [UIImage imageNamed:@"myimage.png"]; 
    lbl1.text = @"howdy"; 
    lbl2.text = @"there"; 
    lbl3.text = @"foo"; 

    return cell; 
} 
+0

이것은 매력처럼 일했습니다! – Andyy

0

당신은 각 셀

[cell.contentView addSubview:imgThumbnail]; 
[cell.contentView addSubview:lblName]; 
[cell.contentView addSubview:lblDescription]; 
[cell.contentView addSubview:lblDate]; 

각 셀은 서브 뷰 객체의 자체적 인 필요에 동일한 서브 뷰 개체를 추가합니다.

0
imgThumbnail 
lblName 
lblDescription 
lblDate 

이러한 객체에 태그를 지정해야합니다 (인터페이스 작성기에서이를 수행 할 수 있음).
그리고 셀을 구성하면 태그 셀을 쿼리하고 속성을 설정합니다.

지금 바로 추가 된 마지막 셀의 레이블에 대한 참조를 유지하고 테이블에서 새 셀을 요청할 때마다 이러한 레이블을 변경합니다.

당신이 다음

UILabel *lblDescription = (UILabel *)[cell viewWithTag:10]; 
lblDescription.text = [dictionary objectForKey:@"Description"]; 

편집과

lblDescription.text = [dictionary objectForKey:@"Description"]; 

대체 할 인터페이스 빌더에서 라벨 lblDescription에 태그 (10)를 할당 말할 수 있습니다 : 나는 imgThumbnail는 등의 파단 것으로 가정하여 셀,하지만 당신은 그들을 다시 추가합니다. 내 가정이 맞다면 [cell.contentView addSubview ...]를 제거해야합니다.

내가 틀렸다면 imgThumbnail 등을 viewcontroller의 인스턴스 변수로 제거해야합니다. 새 셀을 만들 때마다 개별 UIView를 추가하십시오. 당신이 backgroundview로하는 것처럼. 셀 값을 구성 할 때 태그를 지정하고 해당 태그를 사용합니다.

+0

OMG 전설! 그것은 일했다! 나는 Tag와 Object ID와 혼동을 느꼈다. DOH! 하지만 이미지를 넣을 수는 없지만 이미지에 태그를 추가하려면 어떻게해야합니까? – Andyy

+0

아직 그것에 붙어있어, 데모 코드를 게시 할 수있는 기회가 있습니까? thx :) – Andyy

0

을 사용자 정의 할 객체 UIKit.열];

대신

정적는 NSString * CellIdentifier = @ "셀";

+1

여러 질문에 대해 상용구/축 어적으로 답변을 복사하여 붙여 넣을 때는 커뮤니티에서 "스팸성"으로 표시되는 경향이 있으므로주의하십시오. – Kev

1
iOS Swift: I have done following to resolve my issue 

let CellIdentifier: String = "CellIdentifier\(indexPath.section)\(indexPath.row)" 

     var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(CellIdentifier) as UITableViewCell? 

     if cell == nil { 
      cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: CellIdentifier) 
     } 
관련 문제