2012-03-09 111 views
0

我提出其中有內容「靜細胞」和風格「分組」自定義表視圖。我想以編程方式插入靜態內容。我可以做初始化視圖:自定義的UITableViewController結構

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

我想使3個部分表中的圖,其具有2個細胞和具有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]; 
}