当前位置: 动力学知识库 > 问答 > 编程问答 >

iphone - Loading UITableViewController from code

问题描述:

I am having issues with what I can only imagine is a very simple problem. I am loading a UITableViewController class called LocationViewController from one UIViewController:

LocationViewController *lvc = [[LocationViewController alloc] init];

[self.navigationController pushViewController:lvc animated:true];

In this class I have the 3 following method implemented:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

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

{

return 3;

}

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

{

static NSString *CellIdentifier = @"CityCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

cell.textLabel.text = @"Test";

return cell;

}

And I am getting the following error:

UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

I have had this error before when transitioning between ViewControllers using the StoryBoard this was because the CellIdentifier was not correct. I have no idea what I am doing wrong. I have tried loading a nib file with this ViewController but that throws the same error.

网友答案:

You have to allocate the cell. use this code.

static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
网友答案:

use this code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = @"Test";
return cell;}
网友答案:

dequeueReusableCellWithIdentifier returns nil, if there is no object in the reusable-cell queue. check if cell is nil after calling dequeueReusableCellWithIdentifier, create a new UITableViewCell in that case.

网友答案:

dequeueReusableCellWithIdentifier is a cache of already created UITableViewCells with your identifier "CityCell"

If you use the code below, it tries to get a cell from the cache, and if it can't it will create one and then store it in the cell cache for later use.

You need the cache for large tables, as it drastically improves table performance when scrolling.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"CityCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    }
    cell.textLabel.text = @"Test";

    return cell;
}

Take a look at the table view programming guide to learn more about this:

Table Programming Guide

分享给朋友:
您可能感兴趣的文章:
随机阅读: