ios 在 UITableView 中设置多个部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13407194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:13:21  来源:igfitidea点击:

Setting up multiple sections in UITableView

iosuitableviewtableview

提问by StuartM

I have setup a uitableview with custom cells.

我已经设置了一个带有自定义单元格的 uitableview。

I would like these into sections with a title. Looking at the below photo I am looking for the following layout:

我想把这些分成带标题的部分。查看下面的照片,我正在寻找以下布局:

Section - My Profile Custom cell - wwwwwwwwwwwwww...
Section - Application
Custom cell - Games
Custom cell - Share
Custom cell - Rate
Custom cell - Settings
Custom cell - Help
Custom cell - Log out

部分 - 我的个人资料自定义单元格 - wwwwwwwwwwwwww...
部分 - 应用程序
自定义单元格 - 游戏
自定义单元格 - 共享
自定义单元格 - 速率
自定义单元格 - 设置
自定义单元格 - 帮助
自定义单元格 - 注销

I can see how to add a section and control the rows in a section, but this duplicates the cells into multiple sections, I am not sure how to have one section with one row and another section with 6 rows. I also want to style these sections to show, slightly like the Facebook menu style.

我可以看到如何添加一个部分并控制一个部分中的行,但这会将单元格复制到多个部分中,我不确定如何将一个部分包含一行而另一个部分包含 6 行。我还想设置这些部分的样式以显示,有点像 Facebook 菜单样式。

Should I create custom cells for the actual sections instead and have no action on the section (cell) selection?

我是否应该为实际部分创建自定义单元格而不对部分(单元格)选择进行操作?

Here is the code for the UITableView

这是 UITableView 的代码

static NSString *CellIdentifier = @"Cell";
LeftMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"LeftMenuTableViewCell" owner:nil options:nil];

    for (UIView *view in views) {
        if([view isKindOfClass:[UITableViewCell class]]) {
            cell = (LeftMenuTableViewCell*)view;


        }
    }
}

enter image description here

在此处输入图片说明

回答by user427969

You could define number of sections and rows in it as following:

您可以定义其中的部分和行数,如下所示:

- (UIView *) tableview:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
    UIView view;
    if(section == 0) {
         // Initialise view for section 1
    } else {
         // Initialise view for section 2
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

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

    return ((section == 0) ? 1 : 6);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // return appropriate cell(s) based on section
    if(indexPath.section == 0) 
    {
        // Return 1 cell
    }
    else if(indexPath.section == 1) 
    {
        switch(indexPath.row) {
            case 0: // Initialize cell 1
                    break;
            case 1: // Initialize cell 2
                    break;
            ...
        }
    }
    return cell;
}