将未知行数添加到“静态单元格”UITableView

2024-01-02

我在 Interface Builder 中创建了一个静态表,其中包含 6 个部分,每个部分的行数都不同。 我现在想添加具有不同行数的第七部分。

首先,一旦我取消注释 Xcode 插入的标准表委托方法,我就会在 self.tableView.tableHeaderView = containerView; 处崩溃。我在表格中添加了一个标题。

更重要的是我遇到了以下代码的崩溃

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==6) {
        return 4;
    } else {
        return [super tableView:tableView numberOfRowsInSection:section];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{/*
    if (indexPath.section == 6) {
        static NSString *CellIdentifier = @"cellWireless";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        // Configure the cell...

        return cell;
    }*/
    return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}

如何正确地保留现有部分不变,但添加一个带有几个单元格的额外部分?


要将动态单元格添加到静态单元格表​​中,您必须重写每个具有 indexPath 的 UITableView 委托方法。

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

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

.

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
     return NO;
}

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{     
     return NO;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{     
     return UITableViewCellEditingStyleNone;     
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     int section = indexPath.section;

     // if dynamic section make all rows the same height as row 0
     if (section == self.dynamicSection) {
          return [super tableView:tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
     } else {
          return [super tableView:tableView heightForRowAtIndexPath:indexPath];
     }
}

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
     int section = indexPath.section;

     // if dynamic section make all rows the same indentation level as row 0
     if (section == self.dynamicSection) {
          return [super tableView:tableView indentationLevelForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
     } else {
          return [super tableView:tableView indentationLevelForRowAtIndexPath:indexPath];
     }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     if (section == self.dynamicSection ) {
          return [self.dataListArray count];
     } else {
          return [super tableView:tableView numberOfRowsInSection:section];
     }
}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
     int section = indexPath.section;
     int row = indexPath.row;


     if (section == self.dynamicSection) {
          // make dynamic row's cell
          static NSString *CellIdentifier = @"Dynamic Cell";
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

          if (!cell) {
               cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
          }

          cell.textLabel.text = [self.dataListArray objectAtIndex:row];
          return cell;
    } else {
          return [super tableView:tableView cellForRowAtIndexPath:indexPath];
    }
}

只有当您重写了所有方法后,您的表才会开始工作。对于任何引用静态部分的内容,只需参考[super]。

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

将未知行数添加到“静态单元格”UITableView 的相关文章

  • 为什么 Objective-C 方法名称的最后一部分必须带有参数(当有多个部分时)?

    在 Objective C 中 您不能声明最后一个组件不带参数的方法名称 例如 以下内容是非法的 void take id theMoney andRun void take id yourMedicine andDontComplain
  • HTML 分页

    有没有html分页的开源项目 我正在为 iPhone 开发一个应用程序 我想在 UIWebView 上显示 HTML 文件 并且不希望用户向下滚动以查看屏幕上未显示的剩余内容 我想在第二个 UIWebView 上显示剩余的内容 我怎样才能做
  • 如何解决malloc_error_break?

    我在 iOS 3 0 模拟器上遇到此错误 但在 3 1 3 和 3 2 模拟器上没有遇到此错误 创建符号断点后malloc error break 我在日志中看到了这一点 Session started at 2010 02 13 19 1
  • Objective-C 中是否有命名初始化方法的约定?

    在可以通过不同的 init 方法初始化的 Objective C 类中 将所有初始化程序共用的初始化代码收集到一个从其他 init 方法 有时也从从 Nib 唤醒 是否有关于如何命名该方法的约定 初始化器 初始化公共 根据 Apple 的说
  • AVCaptureDevice 找不到任何设备

    这行代码是我今天遇到的问题 macOS 应用程序 NSArray devices AVCaptureDevice devicesWithMediaType AVMediaTypeVideo 我更新Xcode后 系统总是让我空着devices
  • 如何为 NSAttributedString 内的文本设置“隐藏”属性?

    我有一个 Cocoa 应用程序NSTextView控件将其文本保存在NSAttributedString 实际上我相信这是一个NSMutableAttributedString 我可以轻松地在该字符串内的不同字符范围上设置和修改不同的文本属
  • 如何用图片替换UITableView?

    我有一个 UITableView 默认为空白 直到用户编辑并向其添加数据 我想显示一张带有说明的图像 直到用户编辑它为止 图片的大小非常适合导航栏和标签栏之间 有没有办法以编程方式执行此操作 您可以使用removeFromSuperview
  • 如何使用 Core Graphics 在我的触摸位置绘制一个圆圈?

    新程序员来了 我在尝试使用 Core Graphics 在触摸位置周围绘制描边弧时遇到问题 我有绘制圆圈的方法工作正常 并且我已经测试并在点击屏幕时注册触摸 但是当我尝试在点击时调用绘制圆圈的方法时 我收到错误 CG ContextBlah
  • NSManagedObject 的 Xcode 9 构建问题:Date 与 NSDate

    Xcode 9 生成不同的代码Date模拟器与设备中实体的类型属性 我有codegen功能下Class set to category extension在核心数据中 直到 Xcode 8 3 最新 一切都工作正常 NSDate总是 下面是
  • iOS 视图控制器内存在被关闭后未释放

    当用户单击按钮时 它会显示一个带有两个视图控制器的新选项卡栏视图控制器 我是这样做的 ACLevelDownloadController dvc ACLevelDownloadController alloc initWithNibName
  • DatePicker 停止 CoreData 按预期工作

    我有一个应用程序 它保存 UIDatePicker 中的文本和日期 然后在您回到 UIDatePicker 中的该日期时显示该注释 效果很好 只有我发现将 UIDatePicker 日期设置为今天会停止 CoreData 工作 只有当我运行
  • Objective-c 中的块递归

    当执行涉及 Objective C 块的递归时 我在 iOS 应用程序中收到 EXC BAD ACCESS 信号 这是简化的代码 void problematicMethod FriendInfo friendInfo onComplete
  • 如何防止Apple Watch进入睡眠状态?

    我们正在开发一个 Apple Watch 项目 但如果不被打扰 手表就会进入睡眠状态 有什么办法可以阻止它进入睡眠状态吗 据我所知和有关该主题的其他搜索 目前还没有api可通过编程方式启用或禁用 Apple Watch 的睡眠模式
  • iPhone UINavigationBar 使用 [UINavigationBar 外观] 更改所有控制器的字体样式

    我知道我可以单独更改导航栏的字体 如本答案所述 更改导航栏的字体 https stackoverflow com questions 5832036 change the navigation bars font 目前我正在使用一种更全局的
  • 推入 UINavigationController 时隐藏 FBFriendPickerViewController 导航栏

    介绍一个实例FBFriendPickerViewController using presentViewController animated completion 非常简单 该类似乎是针对该用例的 但是 我想推送一个实例FBFriendP
  • 个人帐户开发者之间的 Apple 开发/分发证书

    我一直在到处寻找有关处理证书的正确答案 想象一下以下帐户 Joe拥有个人 Apple 帐户 但他根本不会编码 他只是发布了该应用程序并将其称为自己的 Bob还有一个个人 Apple 帐户 Bob 是一位编码专家 Joe 付费让他开发他的第一
  • 在 Swift 中使用 CommonCrypto 解密时出现问题

    我在一家Swift only加密 解密Extension for String and NSData 并且 crypt 部分的工作基于 Zaph 在链接问题中提供的答案 在 Swift 中使用 CCCrypt CommonCrypt 时出现
  • 频繁绘制 CGPath 时的性能

    我正在开发一个将数据可视化为折线图的 iOS 应用程序 该图被绘制为CGPath在全屏自定义中UIView最多包含 320 个数据点 数据经常更新 图表需要相应地重新绘制 刷新率为 10 秒就很好了 到目前为止很容易 然而 我的方法似乎需要
  • 拖动时获取MKAnnotation的坐标

    我正在根据用户添加的注释的位置创建一条路径 MKPolyline 我想允许用户通过拖动引脚来更改路径 我目前可以做到这一点 但 MKPolyline 不会更新 直到引脚被放下 我实施了 void mapView MKMapView mapV
  • 在故事板中的视图控制器之间滑动手势

    我希望添加左右滑动手势来在视图控制器之间进行更改 这是否可能 并且有没有一种简单的方法可以在故事板中执行此操作 谢谢 故事板允许您在两个视图控制器之间设置 Segues 我想说首先在视图之间附加 Segues 给它一个标识符 然后使用类似的

随机推荐