ios - 在 Xcode 的分组 TableView 中添加部分

标签 ios xcode tableview

我的代码与其他代码有点不同,但它可以工作。

我是应用程序编码新手,但我想添加一些 这些分为几个部分:

enter image description here

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

但是我的代码如下所示:

enter image description here

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

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

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

谁能帮帮我?

最佳答案

你必须实现一些 UITableView 方法和委托(delegate)方法(当然还要将你的类设置为该类的委托(delegate)):

- (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;
}

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

关于ios - 在 Xcode 的分组 TableView 中添加部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8056562/

相关文章:

objective-c - 如何在 Xcode 中运行 OCUnit 测试的子集

java - TableView固定第一列

ios - 如何使用 Swift 处理 tableView 单元格音频播放和暂停按钮?

ios - 如何以编程方式从 UIView 获取约束 "bottomSpace"?

ios - 表格 View 行中的 UISwitch

javascript - 如何在 JavaScript 中播放声音

objective-c - 如何在 ImageView 上方放置自定义按钮>

ios - 更改 NSString 的字体大小并加载到 TextView 中

ios - 循环结束后如何调用函数?

ios - 包含另一个框架的 Swift Cocoa Touch Framework