0

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

Maje
  • 596
  • 4
  • 12

2 Answers2

0

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.

Muhammad Waqas Bhati
  • 2,775
  • 20
  • 25
  • See link above @MuhammadWaqasBhati – Maje Dec 15 '15 at 15:26
  • Can you add all exception break Point and check on which line it is crashing For adding all exception breakpoint check this link if you don't know already http://stackoverflow.com/questions/17802662/exception-breakpoint-in-xcode – Muhammad Waqas Bhati Dec 15 '15 at 15:30
  • @MarckKühme You have another error (it is not datasource or delegate error) Please can you add to your question NSLog(@"Info: %@", responseObject) because if we see what you receive we can help you – Сергей Олейнич Dec 15 '15 at 15:33
  • Your error is in `cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];`. Your `dataArray` is containing `responseObject` which is an array. You cannot directly cast array to string. Try to log the `responseObject` first. – seto nugroho Dec 15 '15 at 15:34
  • Hmm, im not sure how to use it. I've tried to set breakpoints out of the line with `self.tableViewObject.dataSource = self;`, `self.tableViewObject.delegate = self;` and `[self.tableViewObject reloadData];` – Maje Dec 15 '15 at 15:37
  • ok then add a break Point on this line cell.textLabel.text = [dataArray objectAtIndex:indexPath.row]; You are getting Array in response and you are assigning to label – Muhammad Waqas Bhati Dec 15 '15 at 15:43
  • @setonugroho look my latest link^ – Maje Dec 15 '15 at 15:44
  • Which value you want to assign to your label ? – Muhammad Waqas Bhati Dec 15 '15 at 15:45
  • I want to have 1..n cards, so now I've created 1 card and i want i to say "card1" in the first cell and when I tap on it, I want to see a new page with all the values – Maje Dec 15 '15 at 15:49
  • You are getting an Array of Dictionaries in Response Now you have to parse that data and get NSString out of it and assign it to your cell label – Muhammad Waqas Bhati Dec 15 '15 at 16:10
  • I'm not sure how to do that. It's the first time I'm using `UITableView` and `cells` @MuhammadWaqasBhati – Maje Dec 15 '15 at 16:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98001/discussion-between-muhammad-waqas-bhati-and-marck-kuhme). – Muhammad Waqas Bhati Dec 15 '15 at 16:27
0

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.

Community
  • 1
  • 1
seto nugroho
  • 1,359
  • 1
  • 13
  • 20
  • The class I'm doing in it, does it have to be a ViewController og a TableViewController? @SetoNugroho – Maje Dec 17 '15 at 12:20
  • Hmm, I'm not sure how to connect the outlets to get output. I have created a UITableViewController `MCTVC` and looks like this: `@interface MCTVC : UITableViewController { NSArray *dataArray; } @property (strong, nonatomic) IBOutlet UITableView *tableViewObject; @end` But it keeps getting me the same error as before..@SetoNugroho – Maje Dec 18 '15 at 00:06
  • In my code it i try print out `NSLog(@"TableView: %@", _tableViewObject);` Then i get this output: `; animations = { position=; }; layer = ; contentOffset: {0, 0}; contentSize: {320, 0}> `@SetoNugroho – Maje Dec 18 '15 at 00:28
  • why are you print your `_tableViewObject`? did you create `NSDictionary` object in your new view controller? – seto nugroho Dec 18 '15 at 00:32
  • No, cause I dont think the problem is there. It seems to I never get into: `- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)` and `- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath` @SetoNugroho – Maje Dec 18 '15 at 00:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98294/discussion-between-seto-nugroho-and-marck-kuhme). – seto nugroho Dec 18 '15 at 00:36