简体   繁体   中英

tableView does not show up

This problem occured in two of my view controllers.When I ran the app, the tableView does not even show up, not to mention the data that should be displayed in the table. I cannot post a screenshot here cuz my reputation is not high enough.

Here is my code.

#import "OngoingTournamentTableViewController.h"
#import "TournamentTableViewCell.h"
#import "AppDelegate.h"

#define kOngoingTournamentURL [NSURL URLWithString:@"https://www.kimonolabs.com/api/d4leq2cs?apikey=xgp4nU6xA9UcBWSe0MIHcBVbAWz5v4wR"]


@interface OngoingTournamentTableViewController ()

@property (nonatomic, strong) NSArray *ongoingTournament;

@end

@implementation OngoingTournamentTableViewController 

- (void)viewDidLoad {
[super viewDidLoad];

dispatch_async(kBgQueue, ^{
    NSData *data = [NSData dataWithContentsOfURL:kOngoingTournamentURL];
    [self performSelectorOnMainThread:@selector(fetchedTournamentData:)
                           withObject:data waitUntilDone:YES];
    });

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

- (void)fetchedTournamentData:(NSData *)tournamentResponseData {
NSError *error;
NSDictionary *json = [NSJSONSerialization
                      JSONObjectWithData:tournamentResponseData
                      options:kNilOptions
                      error:&error];

NSArray *myOngoingTournament = [[json objectForKey:@"results"] objectForKey:@"collection1"];

self.ongoingTournament = myOngoingTournament;

[self.tableView reloadData];

NSLog(@"%@", self.ongoingTournament);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self.ongoingTournament count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tournamentCell" forIndexPath:indexPath];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"tournamentCell"];
}

NSDictionary *tournaments = [self.ongoingTournament objectAtIndex:indexPath.row];

NSString *title = [[tournaments objectForKey:@"Title"] objectForKey:@"text"];
NSString *venue = [tournaments objectForKey:@"Venue"];

cell.textLabel.text = title;
cell.detailTextLabel.text = venue;

return cell;
}


@end

I have assigned classes to the tableView and prototype cell. Any suggestions?

And I don't get anything on the console, yet the url works fine in the browser.

Thank you in advance and again sorry for being such a burden.

youre going to have to adopt uitableview protocols and set your tableview delegate and datasource.

if the header file of your view controller you will need to look something like this

@interface myViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

then in yor implementation somewhere when setting up your tableview

mytableview.delegate = self;
mytableview.dataSource = self;

You can do that programatically like @myte said, or in storyboard and link your tableview to the controller and then select " Delegate " and do it again with " Datasource ".

Then in the .h modify the @interface line like this :

@interface myViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

Download your data using NSURLConnection like below. Call your f

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:kOngoingTournamentURL];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
    if (error == nil)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
           [self fetchedTournamentData:data]
        });
    }
}];

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