[英]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];
調試方法很簡單。
很可能您會發現numberOfRowsInSection返回錯誤的值。
這是一個帶有tableView或UITableViewController的UIViewController嗎?
以下是您需要檢查的主要步驟:
如果是UITableViewController,請跳過下一個項目。
如果您沒有直接使用UITableViewController,請確保tableView是一個IBOutlet,並將您的tableview連接到Interface Builder上的控制器。 還要確保您的控制器實現協議
在Interface Builder上,將tableview連接到控制器,使控制器成為數據源和委托
在viewDidLoad上定義數據數組后嘗試調用[tableView reloadData]
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
該行需要刪除,但不應該阻止表顯示內容。 它完全違背了重用隊列的目的。
剩下的唯一可能性是沒有在故事板中設置單元標識符,或者它不是@“Cell”。 注釋標識符區分大小寫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.