2012-03-09 4 views
0

'정적 셀'및 스타일 '그룹화'콘텐츠가있는 사용자 지정 테이블 뷰를 만들고 있습니다. 나는 프로그램 적으로 정적 인 내용을 삽입하고 싶다. 내가하여보기 init을 수행 할 수 있습니다사용자 지정 UITableViewController 구조체

MyCustomViewController *myCustomViewController = [[MyCustomViewController alloc] init]; 
[self.navigationController pushViewController:myCustomViewController animated:TRUE]; 

나는 테이블보기에서 3 개 섹션을 만들고 싶어,이 세포와 1 개 셀을 갖는 다른 두 섹션을 가진 하나 개의 섹션. 다이내믹 셀을 이전에 배치했는데이 섹션 생성 및 셀 수의 변화에 ​​대처할 방법이 없습니다. 어떤 해결책?

enter image description here

답변

1

이 당신을 도움이 될 것입니다!

- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 

    NSArray *firstSection = [NSArray arrayWithObjects:@"Red", @"Blue", nil]; 
    NSArray *secondSection = [NSArray arrayWithObjects:@"Orange", @"Green", @"Purple", nil]; 
    NSArray *thirdSection = [NSArray arrayWithObject:@"Yellow"]; 

    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:firstSection, secondSection, thirdSection, nil]; 
    [self setContentsList:array]; 
    array = nil; 


} 
- (void)viewWillAppear:(BOOL)animated 
{ 

    [super viewWillAppear:animated]; 

    [[self mainTableView] reloadData]; 

} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    NSInteger sections = [[self contentsList] count]; 

    return sections; 
} 

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section 
{ 

    NSArray *sectionContents = [[self contentsList] objectAtIndex:section]; 
    NSInteger rows = [sectionContents count]; 

    NSLog(@"rows is: %d", rows); 
    return rows; 
} 

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

    NSArray *sectionContents = [[self contentsList] objectAtIndex:[indexPath section]]; 
    NSString *contentForThisRow = [sectionContents objectAtIndex:[indexPath row]]; 

    static NSString *CellIdentifier = @"CellIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
     { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 

    [[cell textLabel] setText:contentForThisRow]; 

    return cell; 
} 

#pragma mark - 
#pragma mark UITableView Delegate Methods 

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 
관련 문제