views:

1822

answers:

3

I have a View based application that navigates between all views properly. And now I have to add a UITableView to this application, I am unable to do that.

I could add a UITableView, thats fine, but no idea about adding data source to it. I searched a lot and found only examples begin with navigation based applications. I have created array and I have displayed a blank table view also. How to add add contents to table cells, should I have to override those methods in navigation based applications?

I have a UITableView on a UIView like:

    UITableView * aTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
 mainView = aTableView;

Is it possible, any idea? thanks.

EDIT:

mainView = aTableView;

is modified as

[mainView addSubview:aTableView];
+1  A: 

You just set the dataSource property:

UITableView * aTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
aTableView.dataSource = aDataSource;

Note that you can't just set mainView to your tableView and expect it to work. mainView is an ivar or a local variable representing a view, changing it just changes the ivar, it doesn't actually attach or detach any views in the view hierarchy. In order to do that you you actually need to attaching it you using -[UIView addSubview:].

Louis Gerbarg
I set a datasource that I already have, and that is not displaying in the table as table cells. Is there any method I should override? thanks
pMan
That is probably the second issue I mentioned, you aren't adding the tableview into the view hierarchy. Just setting the ivar won't do it, you need to add it as a subview.
Louis Gerbarg
yes, I did that( added as subview ), but I'm not getting anything in the cells.??
pMan
Sorry, it is not entirely clear to me what you are saying is wrong. It now sounds like you see a tableview with blank cells? Obviously you need to implement the relevant UITableViewDataSource methods in your data source or nothing will display. If you have implemented those and nothing is displaying I couldn't begin to tell you what is going on without seeing them.
Louis Gerbarg
A: 

Did you add UITableViewDelegate and UITableViewDataSource in the header file(.h file) of that controller..

For example,

@interface MainViewController : UIViewController < UIITableViewDelegate,
UITableViewDataSource > 
{

} @end

After doing this.. In .m file,

UITableView * aTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; aTableView.dataSource = self; aTableView.delegate = self; [mainView addSubView:aTableView];
Unless you're doing a composite layout, you're best off making the MainViewController class descend from UITableViewController and then use -[UITableViewController initWithStyle:] instead of -[UIView initWithFrame:]
John Franklin
A: 

And you should add the UITableView to a UINavigationController and add the NavigationController's view to your view. Than you can use

[self.navigationController pushViewController:<Your DetailViewController> animated:YES]

If you don't do this, you'll have much work to show your DetailViewController. ;-)

Sandro Meier