简体   繁体   English

TableView不会加载任何数据

[英]TableView won't load ANY data

Pulling out my hair on this one. 拉出我的头发。 I've made so many apps with table views and have been looking at my past apps, but for some reason this table view is too stubborn to show anything... 我已经制作了很多带有表格视图的应用程序,并且一直在查看我过去的应用程序,但由于某种原因,这个表格视图太顽固,无法显示任何内容......

- (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;
}

So basically nothing shows up for me. 所以基本上什么都没有显示给我。 The table view is blank as always even though i set the delegate, and got the table view delegate and data source in the .h file. 即使我设置了委托,表视图仍然是空白的,并且在.h文件中获得了表视图委托和数据源。

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

The way to debug this is straight-forward. 调试方法很简单。

  1. Set a breakpoint in numberOfSectionsInTableView. 在numberOfSectionsInTableView中设置断点。 See if the breakpoint is hit. 查看断点是否被击中。
  2. Make sure that the value returned from numberOfSectionsInTableView is correct. 确保numberOfSectionsInTableView返回的值是正确的。
  3. Set a breakpoint in numberOfRowsInSection. 在numberOfRowsInSection中设置断点。 Make sure the breakpoint is hit. 确保断点被击中。
  4. Make sure that the value returned from numberOfRowsInSection is correct. 确保numberOfRowsInSection返回的值是正确的。
  5. Set a breakpoint in cellForRowAtIndexPath. 在cellForRowAtIndexPath中设置断点。 Make sure that the breakpoint is hit. 确保断点被击中。
  6. Step through the logic in cellForRowAtIndexPath and make sure it's all correct. 逐步执行cellForRowAtIndexPath中的逻辑并确保它是正确的。

Very likely you will discover that numberOfRowsInSection is returning the wrong value. 很可能您会发现numberOfRowsInSection返回错误的值。

Is this a UIViewController with a tableView or a UITableViewController? 这是一个带有tableView或UITableViewController的UIViewController吗?

Here are the main steps you need to check: 以下是您需要检查的主要步骤:

  1. If it is a UITableViewController, skip the next item. 如果是UITableViewController,请跳过下一个项目。

  2. Make sure the tableView is a IBOutlet if you're not working with a UITableViewController directly, and connect your tableview to your controller on Interface Builder. 如果您没有直接使用UITableViewController,请确保tableView是一个IBOutlet,并将您的tableview连接到Interface Builder上的控制器。 Also make sure your controller implements protocols 还要确保您的控制器实现协议

  3. On Interface Builder, connect your tableview to the controller, making the controller the datasource and the delegate 在Interface Builder上,将tableview连接到控制器,使控制器成为数据源和委托

  4. Try calling [tableView reloadData] after you define your data array, on viewDidLoad 在viewDidLoad上定义数据数组后尝试调用[tableView reloadData]

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

That line needs to be deleted, but shouldn't prevent the table from displaying stuff. 该行需要删除,但不应该阻止表显示内容。 It simply defeats the purpose of the reuse queue. 它完全违背了重用队列的目的。

The only possibility that remains is that the cell identifier hasn't been set in storyboard, or it isn't @"Cell". 剩下的唯一可能性是没有在故事板中设置单元标识符,或者它不是@“Cell”。 Note identifiers are case sensitive. 注释标识符区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM