2014-09-25 2 views
0

사용자 정의 프로토 타입 셀이있는 UITableViewController가 있습니다. 프로토 타입 셀 안에는 2 개의 라벨과 이미지가 있습니다. 이미지의 경우 100, 라벨의 경우 101 및 102로 태그가 지정됩니다. 내가 heres는이 방법viewWithTag는 cellForRowAtIndexPath 메서드 내에서 nil을 반환합니다.

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

    if ([indexPath row] == 0) { 
     static NSString *CellIdentifier = @"InfoCell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

     UIImageView *albumArtworkImageView = (UIImageView *)[cell viewWithTag:100]; 
     albumArtworkImageView.image = [self getAlbumArtworkWithSize:albumArtworkImageView.frame.size]; 

     UILabel *albumArtistLabel = (UILabel *)[cell viewWithTag:101]; 
     albumArtistLabel.text = [self getAlbumArtist]; 

     UILabel *albumInfoLabel = (UILabel *)[cell viewWithTag:102]; 
     albumInfoLabel.text = [self getAlbumInfo]; 

     return cell; 
    } else { 

     static NSString *CellIdentifier = @"Cell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 


     MPMediaQuery *audiobookQuery = [MPMediaQuery audiobooksQuery]; 
     MPMediaPropertyPredicate *albumPredicate = [MPMediaPropertyPredicate predicateWithValue: audiobookTitle forProperty: MPMediaItemPropertyAlbumTitle]; 
     [audiobookQuery addFilterPredicate:albumPredicate]; 
     NSArray *albumTracks = [audiobookQuery items]; 

     NSUInteger trackNumber = [[[albumTracks objectAtIndex:(indexPath.row-1)] valueForProperty:MPMediaItemPropertyAlbumTrackNumber] unsignedIntegerValue]; 

     if (trackNumber) { 
      cell.textLabel.text = [NSString stringWithFormat:@"%i. %@", trackNumber, [[[albumTracks objectAtIndex:(indexPath.row-1)] representativeItem] valueForProperty:MPMediaItemPropertyTitle]]; 
     } else { 
      cell.textLabel.text = [[[albumTracks objectAtIndex:(indexPath.row-1)] representativeItem] valueForProperty:MPMediaItemPropertyTitle]; 
     } 


     if ([self sameArtists]) { 

      cell.detailTextLabel.text = @""; 

     } else { 

      if ([[[albumTracks objectAtIndex:(indexPath.row-1)] representativeItem] valueForProperty:MPMediaItemPropertyArtist]) { 
       cell.detailTextLabel.text = [[[albumTracks objectAtIndex:(indexPath.row-1)] representativeItem] valueForProperty:MPMediaItemPropertyArtist]; 
      } else { 
       cell.detailTextLabel.text = @""; 
      } 

     } 

     return cell; 
    } 
} 

내부에 나는 데 스토리 보드

Storyboard

문제를 살펴 그 태그에 액세스하려고하면 내가있는 태그에서보기를 찾아 줄입니다 반환 없음. 전에 이런 유형의 조회를했는데 왜 그들이 nil을 반환하는지 알 수 없습니다. 어떤 도움을 주시면 감사하겠습니다. 나는 디버깅하기에 좋은 방법조차 모르겠다. 저는 C# 개발자로서 객관적인 -c/ios 프로그래밍을 배우려고합니다. 감사!

+0

'[cell.contentView viewWithTag : 101]'(으)로 변경하십시오. 모든 하위보기는 셀의 내용보기에 있어야하며 셀에는 직접 나타나지 않아야합니다. – rmaddy

+0

나는 그것을 시도했다. 그러나 그것은 아직도 돌아 오지 않고있다. 내가 틀리게 할 수있는 다른 건? – user1585542

+0

viewWithTag :가 nil을 반환하는 것을 어떻게 알 수 있습니까? 로깅이나 디버거로 확인 했습니까? – rdelmar

답변

2

대체 방법을 제공합니다. UITableViewCell에 대한 사용자 정의 클래스를 만들고 거기에 뷰를 선언하십시오. 그런 다음 각 셀의 하위 뷰를 이러한 속성으로 연결하고 viewWithTag를 호출하지 않고 직접 액세스 할 수 있습니다. 사용자 정의 셀의 예를 들어

:

@interface MyCustomCell : UITableViewCell 

@property (strong, nonatomic) UIImageView *albumArtworkImageView; 
@property (strong, nonatomic) UILabel *albumArtistLabel; 
@property (strong, nonatomic) UILabel *albumInfoLabel; 

하고 cellForRowAtIndexPath 방법 :

MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
cell.albumArtworkImageView.image = [self getAlbumArtworkWithSize:albumArtworkImageView.frame.size]; 
cell.albumArtistLabel.text = [self getAlbumArtist]; 
cell.albumInfoLabel.text = [self getAlbumInfo]; 

스토리 보드에서 셀에 MyCustomCell을 설정해야합니다.

희망이 도움이됩니다.

+0

답변 해 주셔서 감사합니다. 속성이 IBOutlets이어야합니까? 어떻게 그들을 연결합니까? – user1585542

+0

저는 이것을 구현했으며, iPhone을 가로로 전환하면 작동하지만 세로 모드로 작동하면 작동하지 않는다는 것을 알고 있습니다. 왜 이런 일이 일어날 지 아십니까? 감사!! – user1585542

+0

접속할 수 있었습니까? Command + 클릭하고 소유자 (노란색 아이콘이있는 viewController)에서 하위 뷰 (label, imageView 등)로 끕니다. 인물 모드에서 작동하지 않는 것은 무엇입니까? – Leandro

관련 문제