2012-12-16 2 views
1

자막을 추가하는 더 좋은 방법을 찾고 있는데 현재 두 개의 다른 배열이 있습니다 제목과 같은 배열에 자막을 추가 할 수 있는지 궁금합니다.NSArray에 자막을 추가하는 방법

lodgeList = [[NSArray alloc]initWithObjects: 

      //Abingdon 
      @"Abingdon Lodge No. 48", // This is the Title 
      // I would like to add the subtitle here 

      @"York Lodge No. 12", 

      //Alberene 
      @"Alberene Lodge No. 277", 

      // Alexandria 
      @"A. Douglas Smith, Jr. No. 1949", 
      @"Alexandria-Washington Lodge No. 22", 
      @"Andrew Jackson Lodge No. 120", 
      @"Henry Knox Field Lodge No. 349", 
      @"John Blair Lodge No. 187", 
      @"Mount Vernon Lodge No. 219", 

위의 각 이름에 부제를 어떻게 추가 할 수 있습니까?

답변

6

제목과 부제에 대한 NSString 속성을 가진 클래스를 만듭니다. 객체를 인스턴스화합니다. 배열에 넣는다.

대신에 당신은 또한 NSDictionaries를 사용할 수있는 사용자 정의 클래스의
-(UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //… 
    MyLodge *lodge = lodgeList[indexPath.row]; 
    cell.textLabel.text = lodge.title; 
    cell.detailLabel.text = lodge.subtitle; 
    return cell; 
} 

.

lodgeList = @[ 
       @{@"title":@"Abingdon Lodge No. 48", 
        @"subtitle": @"a dream of a lodge"}, 
       @{@"title":@"A. Douglas Smith, Jr. No. 194", 
        @"subtitle": @"Smith's logde…"}, 
      ]; 

이 코드는 new literal syntax

-(UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //… 
    NSDictionary *lodge = lodgeList[indexPath.row]; 
    cell.textLabel.text = [lodge objectForKey:@"title"]; 
    cell.detailLabel.text = [lodge objectForKey:@"subtitle"]; 
    return cell; 
} 

갖추고 있으며, 실제로 두 솔루션으로 인해 Key-Value-CodingtableView:cellForRowAtIndexPath:의 동일한 구현을 사용할 수 있습니다

-(UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //… 
    id lodge = lodgeList[indexPath.row]; 
    cell.textLabel.text = [lodge valueForKey:@"title"]; 
    cell.detailLabel.text = [lodge valueForKey:@"subtitle"]; 
    return cell; 
} 

프로토 타입 셀에서도 작동합니까?

예, «WWDC 2011, Session 309 — Introducing Interface Builder Storyboarding»에 나와있는 것처럼 UITableViewCell의 하위 클래스를 만들고 모델을 유지하는 속성과 레이블을 반영하는 속성을 지정합니다. 그 레이블은 스토리 보드에 배선되어 있습니다.

+0

U da man! 고마워. – Apps

+0

이 프로토 타입 셀을 사용할 수 있습니까? – Apps

+0

내 편집을 참조하십시오. – vikingosegundo

관련 문제