开发者

UITableView in view based application

I want to add a table t开发者_高级运维o a view which already has a search bar and some text.

If I created a navigation based project, then I could use

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

but I can't do that inside a view based application.

I don't really need multiple section just the one. How do i do this? thanks


To do this, you have to add a UITableView to your viewController, then define the delegate(s) of your tableview like:

tableView.delegate = self;
tableView.dataSource = self;

This way, your table view will call your class to get its data, and to know how to present them. That means your method tableView:numberOfRowsInSection: will be called.


You have to create a tableView (either in code or in Interface Builder), set the delegate and datasource and then add the tableView as a subview of your current view;

So if you did it programmatically (as opposed to Interface Builder) it would look something like:

UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubView:tableView];

and then you need to implement the UITableViewDelegate and UITableViewDataSource Protocols.

TableView Programming Guide


the method you are mentioning is part of the "UITableViewDataSource Protocol Reference" and is only remotely related to the problem you are describing. you can add a tableView (like any other view) via InterfaceBuilder or via code. In code it would look something like this:

UITableView* tableView = [[UITableView alloc] init];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];

(assuming that you are executing this from within a viewController). Remember that you need to implement

– tableView:numberOfRowsInSection:

and

– tableView:cellForRowAtIndexPath:

in your viewController to make this all work (see UITableViewDataSource Protocol Reference)

I suggest you take a look at "Table View Programming Guide for iOS" if you haven't already!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜