当UITableView的父级不是UITableViewController时,UITableView不显示内容

时间:2013-12-24 16:52:38

标签: ios iphone objective-c uitableview

在XCode 5中,当您添加一个包含标签等的多个原型单元格的 UITableViewController 时,会显示内容,因为 UITableViewController 会自动设置数据表视图的源和委托,并向其发送 reloadData 消息。

iOS Developer Library - Basics of Table View Creation

但是当你将 UITableView 拖到 UIViewController 时,我相信你必须创建类似于下面默认方法的实例方法,以便重新加载和制作内容可见。

我的问题是我如何在 UIViewController 中的某个地方定位 UITableView 并设置其数据源和委托并重新加载并正确显示其内容?

以下是UITableViewController的默认代码:

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

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

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

    // Configure the cell...

    return cell;
}

这是我的代码,它与UITableViewController一起使用,但在UIViewController中不适用于它包含的UITableView:

...

@property (nonatomic, strong) NSArray *menuItems;

...

- (void) viewDidLoad 
{
    [super viewDidLoad];
    self.menuItems = @[@"rowOne", @"rowTwo", @"rowThree", @"rowFour"];
}

...

- (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 [self.menuItems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [self.menuItems objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    return cell;
}

4 个答案:

答案 0 :(得分:1)

在您创建单元格的代码中,如果不存在?并且还将单元格标识符设置为静态。

如果cellForRowAtINdexPath中不存在单元格,则应创建单元格,如下所示:

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
     if(cell == nil)
       {
        //Create your cell
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
      }


     //Configure your cell

    return cell;
}

答案 1 :(得分:0)

您可以通过几种方式设置数据源和委托。如果在故事板中添加表视图以查看控制器,只需控制从表视图拖动到视图控制器并选择委托并再次执行并选择数据源。 您还可以在viewDidLoad中创建插座并在视图控制器中执行:

self.tableView.dataSource = self;
self.tableView.delegate = self;

另一种方法是在代码中创建表视图并将其添加到视图控制器视图中。您可以在viewDidLoad中执行此操作:

tableView = [[UITableView alloc] initWithFrame:CGRectMake(TABLE_VIEW_FRAME) style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;

[self.view addSubview:tableView];

如果您想重新加载数据,请致电:

[self.tableView reloadData];

希望得到这个帮助。

答案 2 :(得分:0)

确保您实施UITableViewDelegateUITableViewDataSource协议,并在initWithNibNameinitWithCoder初始值设定项中设置这些协议。

这就是你的.h文件的样子:

@interface TestViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

以下是您需要在.m文件中执行的操作:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
    }
    return self;
}

如果您使用storyboards,请使用下面给出的initWithCode

- (id)initWithCoder:(NSCoder *)aDecoder {
     self = [super initWithCoder:aDecoder];
        if (self) {
            self.tableView.delegate = self;
            self.tableView.dataSource = self;
        }
        return self;
}

答案 3 :(得分:-1)

您只是将单元格出列,但如果dequeueReusableCellWithIdentifier返回nil,则不会创建一个单元格。你应该按照以下方式实施。

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// here you can configure the cell
return cell
相关问题