簡體   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