2012-05-02 2 views
3

URL 연결을 사용하여 콘텐트를 비동기 적으로로드하는 커스텀 UITableViewCell 서브 클래스를 만들고 싶습니다. 이 모든 및 셀 레이아웃을 정의하는 Nib 파일을 처리하는 UITableViewCell 하위 클래스가 있지만 두 연결하는 데 문제가 있습니다. 여기에 내가 tableView:cellForRowAtIndexPath에서 사용하고 코드는 다음과 같습니다UITableViewCell을 자체 Nib로 서브 클래딩하기

static NSString *FavCellIdentifier = @"FavCellIdentifier"; 

FavouriteCell *cell = [tableView dequeueReusableCellWithIdentifier:FavCellIdentifier]; 

if (cell == nil) 
{ 
    cell = [[[FavouriteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FavCellIdentifier] autorelease]; 
} 

cell.requestURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@?%@=%i", URL_GET_POST_STATUS, 
              URL_PARAM_SERIAL, 
              [[self.favourites objectAtIndex:indexPath.row] intValue]]]; 

return cell; 

이것은 setRequestURL 방법으로로드를 처리하는있는 UITableViewCell 하위 클래스에 요청 URL을 제공합니다.

FavouriteCell 클래스에서 나는 initWithStyle:reuseIdentifier: 메서드를 그대로 유지하고 Nib에서 FavCellIdentifier를 식별자로 설정하고 FavouriteCell을 클래스로 설정했습니다. 이제 FavouriteCell 클래스에서 Nib를로드하려면 어떻게해야합니까?

답변

7

nib/xib 파일을 사용하려면 FavouriteCell을 다르게 인스턴스화해야합니다.

이 시도 :

  1. 당신이 대신 XIB의 기본 UITableViewCellFavouriteCell의 서브 클래스로 당신의 UITableViewCell유형을 변경했는지 확인하십시오. 다음을 수행하십시오.
    • Interface Builder의 객체 분할 창에서 셀을 클릭하십시오.
    • 그런 다음 ID 관리자 탭으로 이동하여 사용자 지정 클래스 목록 아래에있는 클래스 선택이 FavouriteCell인지 확인하십시오. 당신이 사용자 정의 UITableViewCell (단계 # 1과 거의 동일한 프로세스)을 표시 할 위치
  2. 변경 File's Owner 속성은 UIViewController 수 있습니다.
  3. FavouriteCellIBOutlet 속성을 UIViewController에 추가하십시오. 원하는대로 이름을 지정하십시오 (전화 번호는 cell입니다).
  4. UITableViewCell의 xib로 돌아가서 파일 소유자의 cell 속성에 대한 IBOutlet을 사용자 지정 UITableViewCell에 연결하십시오.

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

    static NSString *CellId = @"FavCellId"; 
    FavouriteCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId]; 
    if (!cell) { 
     // Loads the xib into [self cell] 
     [[NSBundle mainBundle] loadNibNamed:@"FavouriteCellNibName" 
             owner:self 
            options:nil]; 
     // Assigns [self cell] to the local variable 
     cell = [self cell]; 
     // Clears [self cell] for future use/reuse 
     [self setCell:nil]; 
    } 
    // At this point, you're sure to have a FavouriteCell object 
    // Do your setup, such as... 
    [cell setRequestURL:[NSURL URLWithString: 
        [NSString stringWithFormat:@"%@?%@=%i", 
         URL_GET_POST_STATUS, 
         URL_PARAM_SERIAL, 
         [[self.favourites objectAtIndex:indexPath.row] intValue]] 
    ]; 
    return cell; 
} 
:
  • 프로그래밍 셀을로드하려면이 코드를 사용하여
  • 관련 문제