English 中文(简体)
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).





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签