2012-07-14 1 views
0

예를 들어, 다른 메뉴로 연결되는 설정 메뉴가 있고 모두 동일한 스타일을 가지고 있습니다.다양한 테이블 뷰에서 동일한 UITableView 스타일을 원한다. 어떻게 할 수 있습니까?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = (UITableViewCell *)[self dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier]; 

     cell.contentView.backgroundColor = [UIColor clearColor]; 
     cell.textLabel.backgroundColor = [UIColor clearColor]; 
    } 

    BOOL isFirstRowInSection = NO; 

    if (indexPath.row == 0) isFirstRowInSection = YES; 

    BOOL isLastRowInSection = NO; 

    int numberOfRowsInSection = [tableView.dataSource tableView:self numberOfRowsInSection:indexPath.section]; 

    if (indexPath.section == 0 && indexPath.row == numberOfRowsInSection - 1) { 
     isLastRowInSection = YES; 
    } 

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.bounds]; 
    imageView.image = cellBackgroundImage(cell.bounds.size, isFirstRowInSection, isLastRowInSection); 

    cell.backgroundView = imageView; 

    //cell.textLabel.text = @"This is a cell"; 

    return cell; 
} 

내가이 클래스는 기본 설정 메뉴와 모든 하위 메뉴를 사용할 수 싶지만, 나는 그들이 무엇을 각 셀의 텍스트처럼 자신의 데이터를 제공 할 때 : 테이블 뷰 셀 cusomisation는 다음과 같이 이루어집니다 여기에서 셀 스타일링 방법을 택하고 싶습니다.

어떻게하면됩니까? 현재의 생각은 데이터 소스 프로토콜의 일부를 응답 할 수있는 자신의 대리자로 복제하는 것입니다.

답변

0

이것을 구현할 수있는 한 가지 방법은 "다른 메뉴"(이 tableview가 "연결되는"것)를 가지고이 tableview의 - tableView:cellForRowAtIndexPath: 메서드를 호출하는 것입니다. 즉, 다음과 같이 보일 것입니다 :

ChildTableViewController.m : 당신은해야합니다

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Somehow, you'll need to give this child VC a pointer to the 
    // "root" tableview – I'll pretend that pointer is "rootTableView" 
    return [rootTableView tableView:tableView cellForRowAtIndexPath:indexPath]; 
} 

, 당신은 당신이 메소드로부터 반환 무엇 내용에 대한 지적 당하고 있는지 확인하기 위해 이후 지금 하나의 tableview뿐만 아니라하지만, 많은 사람들이이 방법을 사용하여 세포를 얻고 있습니다. 당신은 셀을 요청 무엇 UITableView을 확인하여이 작업을 수행 할 수 있습니다 :

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = (UITableViewCell *)[self dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier]; 

     cell.contentView.backgroundColor = [UIColor clearColor]; 
     cell.textLabel.backgroundColor = [UIColor clearColor]; 
    } 

    BOOL isFirstRowInSection = NO; 

    if (indexPath.row == 0) isFirstRowInSection = YES; 

    BOOL isLastRowInSection = NO; 

    int numberOfRowsInSection = [tableView.dataSource tableView:self numberOfRowsInSection:indexPath.section]; 

    if (indexPath.section == 0 && indexPath.row == numberOfRowsInSection - 1) { 
     isLastRowInSection = YES; 
    } 

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.bounds]; 
    imageView.image = cellBackgroundImage(cell.bounds.size, isFirstRowInSection, isLastRowInSection); 

    cell.backgroundView = imageView; 

    //cell.textLabel.text = @"This is a cell"; 

    // Now, all your cells have the same formatting; do your custom 
    // data grabbing or whatever you need to do for the different 
    // tableviews 
    if (tableview == [self view]) { 
     return cell; 
    } else if (tableview = [self childTableView1]) { 
     // Do the setup you'd want for your first child tableview 
     return cell; 
    } else if (tableview = [self childTableView2]) { 
     // Do the setup you'd want for your second child tableview 
     return cell; 
    } // More if statements as needed 

    return cell; 
} 

그것은하지만, 단순히 복사, 깨끗하고 당신이 당신의 "아이의 각각의 - tableView:cellForRowAtIndexPath: 방법으로 보여 주었다 방법의 코드를 붙여 넣을 수 있습니다 "tableviews. 너무 길지 않아 두통이 줄어들지 않습니다.

+0

감사합니다. 코드 복사 및 붙여 넣기는 결코 좋은 생각이 아닙니다. 한 곳에서 그것을 바꾸라고 말하면 다른 곳으로 복사하여 붙여 넣기를하지 않는 한 손에 엉망이됩니다. – Andrew

+1

완전히 사실입니다. 첫 번째 해결 방법이 효과가 있는지 알려주세요. 또한 이러한 셀을 자주 사용할 계획이라면'[UITableViewCell formattedCell]'과 같은 것을 호출 할 때 미리 포맷 된 셀을 뱉어내는 UITableViewCell에 카테고리를 구현할 수 있습니다. – ravron

1

UITableViewCell의 특정 하위 클래스를 만들고 셀 자체에서 시각적 특성을 구현할 수 있습니다.

서브 클래스와 인터페이스 빌더에서 nib 파일을 작성하는 경우 당신은 당신이 클래스를 정의한 경우 (MyTableViewCell 말), 그래서

+ (NSString *)cellIdentifier { 
    return NSStringFromClass([self class]); 
} 


+ (id)cellForTableView:(UITableView *)tableView { 
    NSString *cellID = [self cellIdentifier]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 
    if (cell == nil) { 
     NSArray *nibObjects = [[self nib] instantiateWithOwner:nil options:nil]; 
     NSAssert2(([nibObjects count] > 0) && 
        [[nibObjects objectAtIndex:0] isKindOfClass:[self class]], 
        @"Nib '%@' does not appear to contain a valid %@", 
        [self nibName], NSStringFromClass([self class])); 
     cell = [nibObjects objectAtIndex:0]; 
    } 
    return cell;  
} 

같이 보입니다 클래스 메소드를 가지고 IB의 인터페이스를 구축 할 수 있습니다 FileOwner를 MyTableViewCell의 객체로 만들고 다양한 서브 클래스의 시각적 객체와 Bob의 삼촌의 IBOutlet을 갖도록해야합니다.

는 그런 다음 tableViewController의 cellForRowAtIndexPath 방법에 당신은

MyTableViewCell *cell = [MyTableViewCell cellForTableView:tableView]; 

같은 시각적 프리젠 테이션 로직의 대부분은 인터페이스 빌더 코드에서 유지되는이 방법을 사용합니다. 범용 앱을 제작한다면 두 개의 nib 파일을 만들어야하지만 그렇게 어렵지는 않습니다.

관련 문제