2

I'm trying to make an app programmatically that includes a UITableView that makes a list of items based on the files in the app's Documents directory. I have been able to make the files be read into an array _filepathsArray, but the compilation crashes and Xcode throws warnings when I try to use the array to fill the table. Xcode points out problems with the following lines:

_tableView.delegate = self;
_tableView.dataSource = _filepathsArray;

Both of these throw "semantic issues". The first throws

`Assigning to 'id<UITableViewDataSource>' from incompatible type 'NSArray *__strong'`,

while the second throws

`Assigning to 'id<UITableViewDelegate>' from incompatible type 'BrowserViewController *const __strong'`.

If I remove these lines, the app will compile properly (but of course does not use the data to fill the table), so I assume that the problem has to do with these.

I'm a beginner with Objective C and Xcode, so I can't quite figure out what I'm doing wrong here. Thanks for the help.

UPDATE

I have changed the line _tableView.dataSource = _filepathsArray; to _tableView.dataSource = self; as explained by several answers below. Now, both lines throw the same error:

`Assigning to 'id<UITableViewDelegate>' from incompatible type 'BrowserViewController *const __strong'`.

Could this error be the result of the way that the view controller is configured? In the header file, it is defined as a UIViewController

@interface BrowserViewController : UIViewController

I then include a UITableView as a subview.

木川 炎星
  • 3,893
  • 13
  • 42
  • 51

3 Answers3

10

You should declare a UITableViewDataSource, which is an object that implements that protocol and supplies data to your table.

_tableView.dataSource = self;

From Apple Docs

dataSource
The object that acts as the data source of the receiving table view.
@property(nonatomic, assign) id<UITableViewDataSource> dataSource
Discussion
The data source must adopt the UITableViewDataSource protocol. The data source is not retained.

Updated: Please define your class like below as per my first line in the answer that You should declare a UITableViewDataSource:

@interface BrowserViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
san
  • 3,350
  • 1
  • 28
  • 40
  • Thanks for the response. I have made the change, but the other problem remains. I have added further details to the question. – 木川 炎星 Dec 27 '13 at 05:53
  • Updated my answer, please check. – san Dec 27 '13 at 05:58
  • Agree with @san, the issue is: totally misunderstand the datasource's job. The data source is the one who conforms to UITableViewDataSource and deals with your real data. So if your controller can handle the data, you can simply set self to data source. – Danny Xu Dec 27 '13 at 06:02
1

_tableView.dataSource = _filepathsArray; // <- this is the problem because its type is controller not array,

// add this to your interface
@interface BrowserViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>

because you are currently not confirming the UITableView protocols

always use this like

_tableView.delegate = self;
_tableView.dataSource = self;

and your _filepathsArray will be used in numberofRows delegate for getting the row count and int cellForRowIndexPath to show the data like

cell.titleLabel.text = _filepathsArray[indexPath.row];
Retro
  • 3,985
  • 2
  • 17
  • 41
  • Thanks for the response. I have made the change, but the other problem remains. I have added further details to the question. – 木川 炎星 Dec 27 '13 at 05:54
  • @Retro how can I add multiple controllers to the interface? for eg: I already have one custom controller to the interface. Now I want to add `UIViewController` . so it should be something like this `@interface abc: custom,UIViewController` – ASN Aug 10 '16 at 07:21
  • These are the protocol not the controller. You can create new protocol for your controller. – Retro Aug 10 '16 at 10:10
1

You get these warnings because you didn't declare that you would implement the data source and delegate methods in the .h file of the object that you want to be the data source and delegate. Normally, this would be a subclass of UITableViewController or UIViewController, although any object that implements the protocols could be the data source or delegate. A UITableViewController already conforms to those two protocols, so you don't need to declare anything, but if you're using a UIView controller, you should put this (though it's not strictly necessary) in the .h file:

@interface YourCustomClassName : UIViewController <UITableViewDataSource,UITableViewDelegate> 

In the .m file you should set self as both the data source and delegate (again, this is the usual pattern, but one object doesn't have to provide both roles):

self.tableView.delegate = self;
self.tableView.dataSource = self; 
rdelmar
  • 103,982
  • 12
  • 207
  • 218