在 Xcode 的分组表视图中添加部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8056562/
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-09-14 22:22:15  来源:igfitidea点击:

Adding sections in a grouped table view in Xcode

iosxcodetableview

提问by ifraaank

My code is a little different to others, but it works.

我的代码与其他代码略有不同,但它有效。

I am new to app coding, but I would like to add some of these into sections:

我是应用程序编码的新手,但我想将其中的一些添加到部分中:

enter image description here

在此处输入图片说明

So some of them have their own group with a small title to each.

所以他们中的一些人有自己的小组,每个小组都有一个小标题。

But my code looks like this:

但我的代码如下所示:

enter image description here

在此处输入图片说明

and I don't know what to insert to do it right.

我不知道要插入什么才能正确执行。

(The bottom half of that picture is the pictures in the detail view, that shows up in the detail view when you select something from the table view.)

(该图片的下半部分是详细视图中的图片,当您从表视图中选择某些内容时,它会显示在详细视图中。)

(I know Xcode shows errors in my code, but it still works. )

(我知道 Xcode 在我的代码中显示错误,但它仍然有效。)

Can anyone help me?

谁能帮我?

回答by Oliver

You have to implement some UITableView methods and delegate methods (and of course set your class as the delegate of the class) :

您必须实现一些 UITableView 方法和委托方法(当然也将您的类设置为类的委托):

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //Here you must return the number of sectiosn you want
 }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //Here, for each section, you must return the number of rows it will contain
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
     //For each section, you must return here it's label
     if(section == 0) return @"header 1"; 
     .....
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...
    cell.text = // some to display in the cell represented by indexPath.section and indexPath.row;

    return cell;
}

With that, you can arrange your data as you want : One array for each section, one big array with sub arrays, ... as you want. Antthing will be ok as far as you can return the wanted values from the methods above.

有了它,您可以根据需要排列数据:每个部分一个数组,一个带有子数组的大数组,...根据需要。只要您可以从上述方法返回所需的值,Antthing 就可以了。