【问题标题】:iOS tableview's size differs from tableViewWrapperView's sizeiOS tableview 的大小与 tableViewWrapperView 的大小不同
【发布时间】:2016-05-02 14:19:06
【问题描述】:

我有一个 tableView,它在导航栏下总是有一个空白区域,在我在视图层次结构中调试后,我发现 tableView 的大小与 tableViewWrapperView 的大小不同。有什么想法吗?

以下是我遇到问题的 tableViewController 的代码:

#import "BBTCampusInfoTableViewController.h"
#import "BBTCampusInfoManager.h"
#import "BBTCampusInfoTableViewCell.h"
#import <UIImageView+WebCache.h>

@interface BBTCampusInfoTableViewController ()

@end

@implementation BBTCampusInfoTableViewController

extern NSString * campusInfoNotificationName;

- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveCampusInfoNotification) name:campusInfoNotificationName object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.automaticallyAdjustsScrollViewInsets = NO;
    //self.tableView.contentInset = UIEdgeInsetsZero;
    //Retrive all campus infos
    [[BBTCampusInfoManager sharedInfoManager] retriveData:@""];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[BBTCampusInfoManager sharedInfoManager].infoArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.5;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = @"infoCell";
    BBTCampusInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

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

    NSArray *infoArray = [BBTCampusInfoManager sharedInfoManager].infoArray;

    [cell setCellContentDictionary:infoArray[indexPath.row]];

    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];

    return cell;
}

- (void)didReceiveCampusInfoNotification
{
    NSLog(@"Did receive campus info notification");
    [self.tableView reloadData];
}

【问题讨论】:

  • 我读过那个,但他的方法对我不起作用。
  • @Caeser,因为您显然已经尝试了所有方法(并且没有任何方法对您有用),请将链接添加到您已阅读且对您不起作用的方法。否则人们会不断重复惯用的方法,浪费大家的时间。

标签: ios objective-c uitableview


【解决方案1】:

既然你一直说它不起作用,试试这个,它有点丑陋的解决方法,但它有效。 (见图)

  • 只需在表格视图之前添加一些 UI 组件 -

如果表格视图(或滚动视图)是容器视图上的唯一/第一个子视图,则会发生这种情况。

作为一种解决方案,您可以取消勾选 Adjust Scroll View Insets 见下图

【讨论】:

  • 查看我的更新答案 - 但这应该有效 - 可能是您的自动布局设置已关闭
  • 在您的情况下,您在 viewController 中添加了一个 tableView?我只是拖了一个tableViewController。
  • 其实从没见过 UITableViewController 有这样的行为(通常 UIViewController 中的 UITabelView 有这个问题)。您也可以通过任何方式在 InterfaceBuilder 中看到这种差距,或者只是当您在设备上运行应用程序时。可以尝试在 IB 中删除这个 UItableViewController 并拖放新的 UItableViewController。 :)
  • 只有当我在模拟器上运行我的程序时才会出现这种情况,它不会出现在界面生成器中。我已经尝试过多次放弃新的,但没有任何意义:(
【解决方案2】:

如果使用界面生成器,请尝试:

选择视图控制器, 打开属性检查器, “调整滚动视图插图”默认开启。

或以编程方式尝试:

self.automaticallyAdjustsScrollViewInsets = NO;

在viewdidload中

【讨论】:

    【解决方案3】:

    您是否尝试将此代码添加到viewDidload

    self.edgesForExtendedLayout = UIRectEdgeNone;
    

    或者试试这个代码:

    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.5
    }
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
        return UIView(frame: CGRectZero)
    }
    

    【讨论】:

    • 我试过了,但是空白变得更宽了:(
    • @try to edit to 0.5 或者你可以上传你正在使用的类表。
    • 和我没有加代码的情况一样。请查看我更新的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 2011-04-22
    相关资源
    最近更新 更多