2014-03-13 90 views
0

拔掉我的頭髮。我做了與表視圖這麼多的應用程序和一直在尋找我過去的應用程序,但由於某些原因,這表視圖是太固執,顯示什麼...TableView不會加載任何數據

- (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文件中獲得了表視圖委託和數據源,表視圖仍然是空白的。

+1

您需要提供更多信息,您是否正確實施了numberOfRowsInSection,numberOfSectionsInTableView等? _myArray中的數據是什麼?如果正確連接了一切,你是否在使用XIB? –

+0

嗯我不試圖從數組中加載數據,我只想在我的單元上的文本... – doc92606

+0

我會發布更多的代碼。 – doc92606

回答

1

初始化您的數組。 _myArray = [[NSMutableArray alloc] init];

+0

謝謝!這工作完美。我有時候這麼笨...... – doc92606

0

這是用的tableView或一個UITableViewController一個UIViewController?

這裏有您需要檢查的主要步驟:

  1. 如果它是一個UITableViewController,跳到下一個項目。

  2. 確保tableView是一個IBOutlet,如果您不直接使用UITableViewController,並將您的TableView連接到Interface Builder上的控制器。另外,還要確保你的控制器實現協議

  3. 在界面生成器,你的tableview連接到控制器,使得控制器的數據源和委託

  4. 嘗試調用[的tableView reloadData]定義你的數據陣列之後,上viewDidLoad中

+0

這是一個UITableViewController – doc92606

+0

好吧,在這種情況下嘗試調用[tableView reloadData]陣列。 – marcelorocks

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

該行需要被刪除,但不應該防止表顯示的東西。它只是打敗了重用隊列的目的。

剩餘最後一個可能性是,小區標識符尚未在故事板設置,或者它不是@「小區」。注意標識符區分大小寫。

+0

我確定它是故事板中的單元格 – doc92606

1

調試的方法很簡單。

  1. 在numberOfSectionsInTableView中設置斷點。看看斷點是否被擊中。
  2. 確保從numberOfSectionsInTableView返回的值是正確的。
  3. 在numberOfRowsInSection中設置斷點。確保斷點被擊中。
  4. 確保從numberOfRowsInSection返回的值是正確的。
  5. 在cellForRowAtIndexPath中設置斷點。確保斷點被擊中。
  6. 逐步通過cellForRowAtIndexPath中的邏輯並確保它們都是正確的。

很可能您會發現numberOfRowsInSection正在返回錯誤的值。