繁体   English   中英

TableView不会加载任何数据

[英]TableView won't load ANY data

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

- (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文件中获得了表视图委托和数据源。

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

调试方法很简单。

  1. 在numberOfSectionsInTableView中设置断点。 查看断点是否被击中。
  2. 确保numberOfSectionsInTableView返回的值是正确的。
  3. 在numberOfRowsInSection中设置断点。 确保断点被击中。
  4. 确保numberOfRowsInSection返回的值是正确的。
  5. 在cellForRowAtIndexPath中设置断点。 确保断点被击中。
  6. 逐步执行cellForRowAtIndexPath中的逻辑并确保它是正确的。

很可能您会发现numberOfRowsInSection返回错误的值。

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

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

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

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

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

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

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

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

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

暂无
暂无

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

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