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.
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:
If it is a UITableViewController, skip the next item.
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
On Interface Builder, connect your tableview to the controller, making the controller the datasource and the delegate
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.