TableView不会加载任何数据

时间:2014-03-13 00:56:08

标签: ios objective-c uitableview

拔掉我的头发。我已经制作了很多带有桌面视图的应用程序并且一直在查看我过去的应用程序,但由于某种原因,这个表格视图太顽固而无法显示任何内容......

- (void)viewDidLoad
{  
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.datasource = self;

[_myArray addObject:@"Hi"];
[_myArray addObject:@"Hello"];
[_myArray addObject:@"Bye"];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


-  (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:   (NSInteger)section
{ 
return [_myArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSString *currentItem = [_myArray objectAtIndex:indexPath.row];

cell.textLabel.text = @"Hi";

// Configure the cell...

return cell;
}

所以基本上什么都没有显示给我。即使我设置了委托,表视图仍然是空白的,并且在.h文件中获得了表视图委托和数据源。

4 个答案:

答案 0 :(得分:1)

初始化您的数组。 _myArray = [[NSMutableArray alloc] init];

答案 1 :(得分:1)

调试方法很简单。

  1. 在numberOfSectionsInTableView中设置断点。查看断点是否被击中。
  2. 确保numberOfSectionsInTableView返回的值正确无误。
  3. 在numberOfRowsInSection中设置断点。确保断点被击中。
  4. 确保numberOfRowsInSection返回的值正确无误。
  5. 在cellForRowAtIndexPath中设置断点。确保断点被击中。
  6. 逐步执行cellForRowAtIndexPath中的逻辑,并确保它们都正确无误。
  7. 很可能你会发现numberOfRowsInSection返回错误的值。

答案 2 :(得分:0)

这是一个带有tableView或UITableViewController的UIViewController吗?

以下是您需要检查的主要步骤:

  1. 如果是UITableViewController,请跳过下一项。

  2. 如果您不直接使用UITableViewController,请确保tableView是IBOutlet,并将您的tableview连接到Interface Builder上的控制器。还要确保您的控制器实现协议

  3. 在Interface Builder上,将tableview连接到控制器,使控制器成为数据源和委托

  4. 在viewDidLoad上定义数据数组后尝试调用[tableView reloadData]

答案 3 :(得分:0)

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

该行需要删除,但不应阻止该表显示内容。它完全违背了重用队列的目的。

剩下的唯一可能性是没有在故事板中设置单元标识符,或者它不是@“Cell”。注释标识符区分大小写。

相关问题