【问题标题】:Custom UITableViewController structure自定义 UITableViewController 结构
【发布时间】:2012-03-09 21:22:54
【问题描述】:

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

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

我想在表格视图中创建 3 个部分,一个部分有 2 个单元格,另外两个部分有 1 个单元格。之前已经填充了动态单元格,但不知道如何处理这种部分的创建和不同数量的单元格。有什么解决办法吗?

【问题讨论】:

    标签: ios ios5 uitableview


    【解决方案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];
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-27
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-09
      • 2016-02-07
      • 2020-04-15
      相关资源
      最近更新 更多