2012-03-09 5 views
0

コンテンツ 'Static Cells'とスタイル 'Grouped'を持つカスタムテーブルビューを作成しています。私は静的コンテンツをプログラム的に挿入したい。私は1つのセクションは、1つのセルを有する2つのセルと他の二つのセクションを持つ、テーブルビューに3つのセクションを作りたいカスタムUITableViewController構造体

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

:私は、ビューinitを行うことができます。ダイナミクスセルには以前にダイナミクスセルが組み込まれていましたが、このセクションの作成やさまざまな数のセルを処理する方法については考えられませんでした。どんな解決策ですか?

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]; 
}