我正在制作一个自定义表格视图,其内容为“静态单元格”,样式为“分组”。我想以编程方式插入静态内容。我可以通过以下方法初始化视图:

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


我想在表格视图中制作3个部分,一个部分包含2个单元格,另两个部分包含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];
}

关于ios - 自定义UITableViewController结构,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9634630/

10-14 20:18
查看更多