简体   繁体   中英

UITableView doesn't show data from responseObject

I'm trying to show a responseObject in a TableView , but nothing shows. I can print out all the responseObject 's values, but nothing shows in the TableView when running the app. In the .m file I have this in the ViewDidLoad:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@",access_token] forHTTPHeaderField:@"Authorization"];
[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //NSLog(@"Info: %@", responseObject);
    dataArray = [[NSArray alloc]initWithObjects:responseObject, nil];

    //[self.tableViewObject reloadData];
    self.tableViewObject.dataSource = self;
    self.tableViewObject.delegate = self;



    NSLog(@"TableView: %@", _tableViewObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

If I uncomment [self.tableViewObject reloadData]; it gives me this error:

libc++abi.dylib: terminating with uncaught exception of type NSException

And my tableView in the .m file looks like:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
    return cell;
}

The .h file looks like this:

#import <UIKit/UIKit.h>

@interface MyCardsVC : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *dataArray;
}

@property (weak, nonatomic) IBOutlet UITableView *tableViewObject;



@end

I don't know what I'm doing wrong. I should have connected it right...

You have not assign any datasource and delegate and you are reloading tableView now You have to first assign dataSource and then reload TableView Like this

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@",access_token] forHTTPHeaderField:@"Authorization"];
[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//NSLog(@"Info: %@", responseObject);
dataArray = [[NSArray alloc]initWithObjects:responseObject, nil];

self.tableViewObject.dataSource = self;
self.tableViewObject.delegate = self;

[self.tableViewObject reloadData]; 


NSLog(@"TableView: %@", _tableViewObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

Hope it will help you.

First, you need to modify your request code as suggested by Muhammad Waqas Bhati .

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@",access_token] forHTTPHeaderField:@"Authorization"];
[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //NSLog(@"Info: %@", responseObject);
    dataArray = [[NSArray alloc]initWithObjects:responseObject, nil];

    self.tableViewObject.dataSource = self;
    self.tableViewObject.delegate = self;

    [self.tableViewObject reloadData];

    NSLog(@"TableView: %@", _tableViewObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Since you want to show only card1..cardn in your cell, you should modify your cellForRowAtIndexPath to be like this.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    NSDictionary *objectDict = [dataArray objectAtIndex:indexPath.row];

    /* if you want the label to follow card state, use the first cell.textLabel.text option
       if you prefer using table view row, use the second cell.textLabel.text
    */
    cell.textLabel.text = [NSString stringWithFormat:@"Card%d", [objectDict valueForKey:@"cardState"]];
    //cell.textLabel.text = [NSString stringWithFormat:@"Card%d", indexPath.row];
    return cell;
}

The next step is implementing didSelectRowAtIndexPath delegate.

This delegate is used when you tap the cell, since you want to open new page, the new page must know which cell that is being tapped and the information it contains.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *objectDict = [dataArray objectAtIndex:indexPath.row];
    NSLog(@"%@", objectDict);
    // Pass object to new page
}

If you aren't sure how to pass variable between view, this could help you.

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