Question

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.

Était-ce utile?

La solution

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>

Autres conseils

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; 

_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];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top