ios - 如何使用作为 IBOutlet 连接的 UIView 作为 TableView 部分标题?

标签 ios objective-c uiview

我的 View Controller 是这样设置的,我的所有 UIView 都在 XIB 中,

@property (weak, nonatomic) IBOutlet UIView *tableHeader, *likesHeader, *commentsHeader, *followersHeader;

当 View 加载时,所有可见的节标题都正确显示,但一旦我滚动它们就会消失。我使用委托(delegate)方法来设置节标题。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return self.tableHeader;
    } else if (section == 1) {
        return self.likesHeader;
    } else if (section == 2) {
        return self.commentsHeader;
    } else if (section == 3) {
        return self.followersHeader;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 120.0;
    } else {
        return 35.0;
    }
}

我怎么搞砸了?

最佳答案

您可能需要将导出从 weak 更改为 strong

由于您的标题 View 不是作为父 View 的 subview 开始的,因此没有其他内容引用它们。因此,您的 weak outlets 将变为 nil,header views 将被释放。

您可能会成功返回最初可见的 header ,因为 nib 加载程序可能会将它们放入自动释放池中,直到 TableView 请求它们后才会被耗尽。当标题离开屏幕时, TableView 将释放它们。由于没有其他东西保留它们,因此它们被释放并且系统将您的弱导出设置为零。

关于ios - 如何使用作为 IBOutlet 连接的 UIView 作为 TableView 部分标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26124501/

相关文章:

ios - Objective-C自特定日期以来返回的年份和天数

objective-c - mainBundle() 如何在 swift 或 objective-c 中返回对象?

iphone - 我的 subview 之外的触摸事件应该导致 subview 消失

ios - 在 viewDidLoad 之前执行一个函数

c# - Xamarin Forms 如何制作全局工具栏?

ios - 在 iOS 开发的上下文中,什么是配置/makefile 脚本的好书或引用?

objective-c - 画一个没有纹理的圆

ios - 在具有透明 View 的 UITableview 中显示图像列表

ios - 选择器 'circleOfButtons:buttonSize:radius:' 没有已知的类方法

javascript - 平板电脑/ipad 的 window.onzoom 事件?