-1

So I have a UIViewController with table view in it. I did this one without storyboard.

In this viewController I have a HTTPResponse method where i create tableview and assign delegate and datasource to it.

 - (void)APIDownload:(APIDownload*)request {

CJSONDeserializer *deserializer = [CJSONDeserializer deserializer];
NSError *error = nil;
NSDictionary *data = [deserializer deserializeAsDictionary :request.downloadData error:&error];

if (error) {
    [[iToast makeText:@"serious error"] show];
} else {
    if (data && data[@"status"] && [data[@"status"] isEqual:@"ok"]) {
        self.categoriesArray = data[@"data"];

        self.rootTable = [[UITableView alloc] initWithFrame:self.slideContainer.frame
                                                              style:UITableViewStylePlain];
        self.rootTable.delegate = self;
        self.rootTable.dataSource = self.categoriesArray;

        [self.slideContainer addSubview:self.rootTable];

        [self.rootTable reloadData];
        [self.rootTable setHidden:NO];

    } else {
        [[iToast makeText:[NSString stringWithFormat:@"%@", data[@"data"][@"message"]]] show];
    }
}
}

I have implemented all four necessary methods for uitableviewdelegate and uitableviewdatasource, but still getting

 2015-07-27 12:57:29.073 xxx[10663:602881] -[__NSCFArray tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7f9e10490ae0
 2015-07-27 12:57:29.077 xxx[10663:602881] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7f9e10490ae0'
user3046185
  • 139
  • 2
  • 8
  • You need to go through the [Table View Programming Guide](https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableView_iPhone.pdf). You can't simply assign an `NSArray` as a `dataSource`, you have to implement it. – n00bProgrammer Jul 27 '15 at 10:39
  • I found another cause for this error [here](https://stackoverflow.com/a/65619591/4995828). – Pranav Kasetti Jan 07 '21 at 20:51

3 Answers3

5

The problem you getting is in this line:

self.rootTable.dataSource = self.categoriesArray;

you set up table data source to the array. I believe you want to set it up to self:

self.rootTable.dataSource = self;

Just remember to implement the data source methods in your view controller.

Greg
  • 25,317
  • 6
  • 53
  • 62
0

This line in your code is incorrect:

self.rootTable.dataSource = self.categoriesArray;

Change it to: self.rootTable.dataSource = self;

because the datasource of a UITableView cannot be an NSArray instead it should be an instance that implements the dataSource protocol of the UITableView class. In addition to it in your .m file replace the line @interface YourClass() with @interface YourClass() Hope this will resolve the issue.

Adeel Miraj
  • 2,472
  • 3
  • 22
  • 32
0

Problem lies here:

self.rootTable.dataSource = self;

instead of

self.rootTable.dataSource = self.categoriesArray;

tableview's delegate and datasource are delegation variables that are used for handling protocol methods that define some methods which we override. So only class objects can be set as them but not class variables

Detailed knowledge here:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/index.html

Vaibhav Khatri
  • 101
  • 3
  • 9