ViewController 中的 TableView 不显示单元格

Posted

技术标签:

【中文标题】ViewController 中的 TableView 不显示单元格【英文标题】:TableView within ViewController not displaying cells 【发布时间】:2013-02-06 19:56:26 【问题描述】:

我是 ios 开发的新手,我已经为此苦苦寻找解决方案大约一天了,但不知道为什么它不起作用。我正在尝试将视图控制器中的表格视图用作用户使用的小菜单。我已经检查了 NSArray 是否正在被填充,并且确实如此。我还检查了是否正在创建单元格,并且确实如此。我只是想不通为什么它没有用它创建的单元格填充表格视图。以下是我到目前为止的代码。提前感谢任何人都可以提供的任何帮助。

MainViewController.h

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *menuTableView;
@property (weak, nonatomic) IBOutlet UIButton *menuButton;
@property (strong, nonatomic) NSArray *menuItemsArray;
@property (weak, nonatomic) IBOutlet UILabel *menuLabel;


@end

MainViewController.m

#import "MainViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController
@synthesize menuItemsArray, menuTableView, menuButton, menuLabel;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        // Custom initialization
    
    return self;


- (void)viewDidLoad


    [super viewDidLoad];

    //Set TableView Delegate/DataSource to self
    [self.menuTableView setDelegate:self];
    [self.menuTableView setDataSource:self];
    [self.menuTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.menuTableView setBounces:NO];
    [self.menuTableView setRowHeight:self.menuLabel.frame.size.height];

    self.menuItemsArray = [[NSArray alloc] initWithObjects:@"Add Category", @"Add Item", @"Settings", nil];

    NSLog(@"array: %@", menuItemsArray);


- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);


#pragma mark - UITableViewDelegate

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

    return ([self.menuItemsArray count]);


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"menuCell"];


    [[cell textLabel]setText:[self.menuItemsArray objectAtIndex:indexPath.row]];

    [[cell textLabel]setFont:[self.menuLabel font]];

    return cell;


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

    [self.menuTableView deselectRowAtIndexPath:indexPath animated:YES];

    NSString *selectedString = [self.menuItemsArray objectAtIndex:indexPath.row];

    self.menuLabel.text = selectedString;


【问题讨论】:

行高设置为多少? 当前行高为44 你能用“现代”的Objective-C语法吗?使用越来越少的可读代码,这会更容易,例如: cell.textLabel.text = self.menuItemsArray[indexPath.row];检查 Xcode 菜单编辑 - 重构 -viewDidLoad 的其余部分之后添加[self.menuTableView reloadData]; 会有所帮助吗? 确保在您的 nib 文件中设置了表格视图的出口,否则无论如何您都不会将委托/数据源设置为表格。我也不确定设置行高的那行代码。 【参考方案1】:

我遇到了同样的问题,我的表格视图没有显示在视图控制器中。 我找到了解决方案。 您可以创建另一个带有 Container 视图的视图控制器。并将您的表视图放在表视图控制器上。只需将表格视图控制器嵌入到您的邮件视图控制器的容器视图中。

【讨论】:

【参考方案2】:

确保您的 initWithNib 方法正在被调用。如果您调用[[MainController alloc] init],您的“menuTableView”将永远不会从 Nib 创建。此外,通过将主表视图的backgroundColor 设置为[UIColor red] 或只是为了确保 tableView 存在并且它具有您期望的框架来仔细检查表视图。它可能位于您的其他视图之一的后面,具有(0,0,0,0) 的框架,或者根本不存在于视图中。

还可以尝试在“viewDidLoad”末尾调用[self.menuTableView reloadData] 或在设置数据源和委托之前初始化menuItemsArray(即在您的initWithNib 方法中)。

当你完成所有工作(你非常接近)时,你会想要将你的 cellForRow 方法更改为更像这样的东西:

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

    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:@"menuCell"];
    if(!cell) 
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"menuCell"];
    

    [[cell textLabel]setText:[self.menuItemsArray objectAtIndex:indexPath.row]];

    [[cell textLabel]setFont:[self.menuLabel font]];

    return cell;

这将允许您利用单元重用,使表格视图如此高效。

【讨论】:

感谢您的回复。我一直在尝试使用 Storyboard,但它一直没有工作。我改用 .xib 文件,它工作得很好。关于为什么它适用于一个而不是另一个的任何想法? 对不起,伙计……我想我是老派。我不使用故事板来避免这些类型的情况。很高兴你成功了。【参考方案3】:

这有点晚了,因为您找到了解决方法,但我遇到了和您一样的问题,发现我需要将 IBOutlet 属性连接到情节提要中的表格视图,然后一切正常。

我希望这对您将来有所帮助。

【讨论】:

【参考方案4】:

所描述的症状的一个原因是,如果您使用情节提要中的容器视图将 UITableView 放置在父视图中,但在代码中初始化和填充的 UITableView 实例与实际呈现给的实例不同用户。如果您使用容器视图将 UITableView 放置在视图中,则需要执行以下操作:

使用 segue 将 UITableView 连接到容器视图,方法是从容器视图按住 Control 并拖动到 Storyboard 中的 UITableView。 单击 segue,并为其命名,例如tableViewSegue

通过实现prepareForSegue:sender:来设置表

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

NSString * segueName = segue.identifier;
if ([segueName isEqualToString: @"tableViewSegue"]) 
    UIViewController * myTableView = [segue destinationViewController];
    // Do any table setup here, such as injecting the data array into a property on the tableView.
    

如果您一直在代码中创建不同的 UITableView,您将看到一个未填充的 UITableView,它遵循故事板中设置的规范(例如,行高间距将是正确的)并且响应用户交互,但是是空的。空的是情节提要自动为您初始化的,同时您一直在其他地方创建另一个 UITableView:

// DON'T DO IT THIS WAY IF YOU'RE USING STORYBOARD.
- (void)viewDidLoad 
    [super viewDidLoad];

    // Incorrectly creating a tableview child table view that won't be the one presented.
    self.myTableView = [MYTableViewClass new];
    // ...further configuration of the table.

如果您遵循此错误路径,则您正在创建的另一个 UITableView 正在内存中构建,并填充了您的数据数组,因此您将看到该进程中的所有 NSLog 语句,并能够在内存中看到 UITableView当您逐步执行执行代码时,对象的正确数量等等,但难以理解的是 您没有看到呈现给用户的对象。所以追踪起来可能很棘手。 :)

只需删除上面的代码,实现prepareForSegue:sender:,宇宙就会恢复到可预测的状态。

【讨论】:

【参考方案5】:

如果在UIViewController里面添加UITableView,需要设置UITableView的frame size和UIViewController里面view的frame size一样,否则tableview size可能为0,无法显示。

如果您在案例中通过情节提要创建 UITableView,则可以设置帧大小:

- (void)viewDidLoad 
        [super viewDidLoad];
        // Set tableview delegate and datasource here
        menuTableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

【讨论】:

以上是关于ViewController 中的 TableView 不显示单元格的主要内容,如果未能解决你的问题,请参考以下文章

将 ViewController 嵌入 Storyboard 中的另一个 ViewController

需要从另一个viewController调用其他viewController中的方法

关闭以编程方式呈现在嵌套在 NavigationController 中的 ViewController 中的 ViewController 崩溃,但并非每次

在另一个 ViewController 中的 ViewController 之间显示和切换

如何将 ViewController.swift 连接到 Storyboard 中的 ViewController?

从 XIB Viewcontroller 中的 segmentcontrol 到 Storyboard Viewcontroller