I made a simple code for tableView to display some player data; however, I got the empty table all the time.
This is a table with data fetched from the site.
I want to show the table with player number(as section) and player name(as row).
But after I run the program, it comes out the empty cells and seems no fetching attempt.
Can anyone tell me what’s wrong with my code? Thanks a lot.
#import "playerName.h"
@interface playerName ()
@end
@implementation playerName
@synthesize players = _players;
@synthesize sectionInRtf = _sectionInRtf;
- (NSMutableDictionary *)getPlayerFormRtf
{
if (!_players) {
NSURL *url = [NSURL URLWithString:@"file_on_site"];
_players = [NSMutableDictionary dictionaryWithContentsOfURL:url];
}
return _players;
}
- (NSArray *)getSections
{
if (!_sectionInRtf) {
_sectionInRtf = [self.players allKeys];
}
return _sectionInRtf;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Players";
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.sectionInRtf.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *namesInSection = [self.players objectForKey:[self.sectionInRtf objectAtIndex:section]];
return namesInSection.count;
}
//Additional Method
- (NSString *)name:(NSIndexPath *)indexPath
{
NSArray *nameInSection2 = [self.players objectForKey:[self.sectionInRtf objectAtIndex:indexPath.section]];
return [nameInSection2 objectAtIndex:indexPath.row];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"players";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = [self name:indexPath];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
@end
This looks like you are trying to override the accessor methods but you are not using the correct naming. I make this assumption since you never actually call
getPlayerFormRtf.The code should work (though not well) with two simple modifications.
PLEASE note that this is an extremely bad idea, referring to the synchronous loading of a url, since your placeholder url string seems to imply a network access url. This risks blocking the main thread long enough for the application to be killed.