在 XCode 中向表视图 (UITableView) 添加单元格

2024-03-31

我是 X-Code 新手,刚刚开始开发我的第一个应用程序。我正在使用故事板。在导航控制器场景中,我添加了一个带有两个单元的 MasterViewController,这导致了两个 DetailViewController(Detail1 和 Detail 2)。

  • 列数 = 1
  • 列中的行数 = 2

我在属性检查器中为单元格添加了一个重用指示器,并在故事板中进行了连接。

但是当我运行应用程序时,我只能看到第一个单元格 x2。

当我查看 MasterViewController.m 时,我可以看到第一个单元格的代码,但我不明白如何插入第二个单元格等!

我知道解决方案很简单,我已经寻找答案 3 天了,我觉得很愚蠢,但我现在真的需要一些帮助,,

有人可以帮助我了解如何在代码中添加更多单元格,并且可能更多地了解表视图代码吗?

这是到目前为止表视图的代码:

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Formal";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...

return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath][withRowAnimation:UITableViewRowAnimationFade];
}   
else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
 // ...
 // Pass the selected object to the new view controller.
 [self.navigationController pushViewController:detailViewController animated:YES];
 */
}

@end

假设您有一个名为 arr 的数组。

然后,当调用 numberOfRowsInSection 时,您必须返回数组中的对象数。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [arr count];
}

您的 cellForRowAtIndexPath 委托方法应如下所示,以打印出数组中的字符串。

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

static NSString *CellIdentifier = @"Formal";

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

// Configure the cell.

cell.textLabel.text = [arr objectAtIndex:indexPath.row];

    return cell;
}

希望这可以帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 XCode 中向表视图 (UITableView) 添加单元格 的相关文章

  • 提交的代码签名应用程序无效

    我尝试向应用程序商店提交应用程序 但收到以下错误 我见过类似的问题 并提出了 2 个不同的解决方案 但都没有解决问题 无效的代码签名权利 您的应用程序包的签名包含不受支持的权利值 具体来说 不支持密钥 com apple developer
  • ShareKit 集成期间出现奇怪的编译器错误

    我尝试将 ShareKit 集成到我的项目中 但遇到了问题 当我将 ShareKit 类包含在我的类项目文件夹中时 编译器会出现如下错误 解析问题 未知类型名称 NSUInteger 或 解析问题 未知类型名称 NSString 在 MyP
  • 已删除的测试仍保留在 Xcode 测试导航器中

    我最近从我的项目中删除了一些旧的测试类并删除了这些文件 正如预期的那样 文件被移至垃圾箱并在 git 中显示为已删除 不幸的是 它们定义的测试类和测试用例继续出现在测试导航器中 我尝试过常见的方法 例如清理和重建 以及退出并重新打开 Xco
  • 在 xcode 中使用线程调用函数

    我在 xcode 中创建了一个线程 并且给出了从该线程调用的函数名称 但我的问题是 给 call 的函数名称没有被调用 知道何时在该函数中放置断点 code NSThread myThread myThread start self per
  • iPhone——是否可以在 Xcode 调试器中检查 UIView 的框架?

    当调试器在断点处停止时 我在那里找不到任何 UIView 的框架 是否有可能做到这一点 编辑 由于缺乏回应而开始赏金 需要明确的是 我正在寻找一种在不添加额外调试代码的情况下查看框架的方法 另外 如果答案是 不 你不能这样做 赏金将用于解释
  • iOS App Today 扩展未上传到物理设备

    我正在为我的应用程序创建一个今日小部件http budgt ch http budgt ch因为一些用户要求快速访问关键功能 初步 扩展在 iOS 模拟器上运行良好 安装如下 1 安装最新的容器应用程序 2 安装以 今天 为容器的扩展 但是
  • 进入/退出编辑模式时重绘 UITableViewCell

    我有一个表格视图 其中根据表格是否正在编辑 单元格的构建方式有所不同 具体来说 处于编辑模式时选择样式为无 非编辑模式时选择样式为蓝色 当我从一个单元转换到另一个单元时 我注意到某些单元格没有更新 快速的日志记录告诉我 即使单元格的外观发生
  • iPhone UITableView 分页结果

    对从服务器拉取的大量结果进行分页的最佳方法是什么 就服务器而言 我可以抵消和限制结果 因此我一次只能提取 25 个结果 但是允许用户查看更多结果而不需要像应用商店一样不断向下滚动不断增长的列表的最佳方式是什么应用程序 谢谢 豪伊 要在列表底
  • 部署目标是什么意思?

    这是我假设的一个非常简单的问题 有人可以告诉我部署目标是什么意思吗 如果我选择 iOS 10 是否意味着只有 iOS 10 的用户才能下载该应用程序 选择较低的部署目标是否不好 另外 继续部署目标 是否不建议在较低的部署目标上运行 假设您已
  • UITableview 中的水平和垂直滚动[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 I want to make a lineup for a festival You can see what I want to a
  • 自定义字体显示在 IB 中,但不显示在模拟器中

    我已经设置了一个UITextView and a UILabel使用自定义字体 它是垂直镜像的蒙古文字体 但我还添加了英文文本 以便您可以看到效果 这些文字显示在 Interface Builder 中 但在模拟器中大部分字符都在UITex
  • 是否有针对不同屏幕尺寸的单独故事板?

    基本上我已经完成了一个应用程序 我唯一的问题是 ATM 机应用程序在设计时只考虑了 4 英寸显示屏 当在 3 5 英寸模拟器上运行时 应用程序会丢失 0 5 英寸 显然 那么我的问题是 如何在 Xcode 5 中为不同的屏幕尺寸设置不同的故
  • 如何禁用 UITableView 中某些行的删除操作?

    我知道使用setEditing 启用UITableView的编辑模式 但我更喜欢禁用某些特定行的操作 启用其他行 是否可以 Thanks interdev 实施the tableView canEditRowAtIndexPath meth
  • 在 OS X 上创建和使用静态库

    好的 我正在尝试创建一个 Cocoa 库 静态 并使用 但我不断收到错误 我创建了一个超基本的静态库 TSXLib 其中仅包含一个额外的类 import
  • 覆盖层不与 UITableView 一起滚动 - iOS

    我有一个 UITableView 类 它使用以下方法在转到下一个屏幕时调用加载覆盖 问题是这个加载屏幕不随列表滚动 所以如果你滚动一点并单击某些东西 加载屏幕不会显示 因为它位于顶部 如何让加载屏幕始终保持在 UITableView 的顶部
  • 如何将 ios7 通用应用程序升级到基于 Xcode 6 的通用故事板应用程序?

    我目前有一个基于 xcode 5 ios 7 的通用应用程序 因此有两个故事板 我正在考虑将其更新到 ios 8 有没有办法 最佳方法将两个故事板迁移到通用的单个故事板 我在 xcode 6 中看不到转换选项 None
  • Xamarin - 错误:dsymutil 退出,代码为 72

    最近升级到 VS for Mac 8 10 21 在构建应用程序时 我得到 Xamarin Shared targets 3 3 Error dsymutil exited with code 72 这是 Xcode 13 3 的情况 完整
  • 通过 Button Swift 中的标签发送行和部分

    我里面有这个cellForRowAtIndexPath cell plusBut tag indexPath row cell plusBut addTarget self action plusHit forControlEvents U
  • iOS:Facebook 登录访问令牌错误:由于模拟器错误,回退到从 NSUserDefaults 加载访问令牌

    根据说明进行配置后 我不断收到此错误 并且无法在我的应用程序上成功使用 Facebook 登录 我在 XCode 8 1 上运行它并使用 iOS 10 1 模拟器 我按照 Facebook iOS SDK 指南中的步骤操作 并将 Faceb
  • 如何在 UITableView 的 switch 语句中创建变量?

    我正在构建一个包含三个部分的 tableView 我已经完成了前两个工作 但最后一个有点阻力 我的问题似乎涉及尝试在 switch 语句中声明变量 实际上是嵌套的 switch 语句 据我所知 这不是一个好主意 但在这种情况下 这似乎是唯一

随机推荐