how to add UITableView to a view based application: iPhone sdk question
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];
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:]
.
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];
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. ;-)
In addition to referencing the source and delegate in the header you can connect your datasource and delegate to your 'File's Owner' (using connections inspector) while your editing your XIB (as opposed to doing so manually in the code).
精彩评论