简体   繁体   中英

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.

Initialize your array. _myArray = [[NSMutableArray alloc] init];

The way to debug this is straight-forward.

  1. Set a breakpoint in numberOfSectionsInTableView. See if the breakpoint is hit.
  2. Make sure that the value returned from numberOfSectionsInTableView is correct.
  3. Set a breakpoint in numberOfRowsInSection. Make sure the breakpoint is hit.
  4. Make sure that the value returned from numberOfRowsInSection is correct.
  5. Set a breakpoint in cellForRowAtIndexPath. Make sure that the breakpoint is hit.
  6. Step through the logic in cellForRowAtIndexPath and make sure it's all correct.

Very likely you will discover that numberOfRowsInSection is returning the wrong value.

Is this a UIViewController with a tableView or a UITableViewController?

Here are the main steps you need to check:

  1. If it is a UITableViewController, skip the next item.

  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. 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

  4. Try calling [tableView reloadData] after you define your data array, on viewDidLoad

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". Note identifiers are case sensitive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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