[英]UITableViewController in UIView
I have a plain project with blank UIView
. 我有一个带有空白UIView
的普通项目。 Later, when I'm get some useful information from my serwer i would like to create UIView or UITableView
. 稍后,当我从服务器中获得一些有用的信息时,我想创建UIView或UITableView
。
I have a class: 我有一堂课:
@interface MyTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
}
And I have AppDelegate
, ViewController
and .xib
file. 我有AppDelegate
, ViewController
和.xib
文件。
What i should do to load a UITableView
? 我应该怎么做才能加载UITableView
? I should unload my standard ViewController in AppDelegate or Load TableView inside View as a Subview (possible?) Or i should throw out xib file at all ? 我应该在AppDelegate中卸载我的标准ViewController或将View内的TableView加载为子视图(可能吗?),或者我应该扔掉xib文件吗? Of course everything i want to do in to my Views has to be programically not using IB. 当然,我要对视图执行的所有操作都必须以编程方式不使用IB。
In my app i want to create both, depends on receiving data. 在我的应用程序中,我想创建两者,取决于接收数据。
I would create a uiview and load the uitableview inside the uiview (rootViewController) with that said, I assume you are using interface builder (IB) which makes things much more visual and easy to manipulate visually. 我会创建一个uiview并在uiview(rootViewController)中加载uitableview,上面说的是,我假设您使用的是界面生成器(IB),它使外观更直观并且更易于视觉操作。 If needed I will add the relevant code. 如果需要,我将添加相关代码。
you can push a UITableViewController Or a UIViewController contain a UITableView 您可以推送UITableViewController或包含UITableView的UIViewController
or juse add a tableView to current view of UIViewController 或juse将tableView添加到UIViewController的当前视图
UITableView tableView = [[UITableView alloc] initWithFrame:self.view.bound];
[self.view addSubView:tableView];
write this code in .h file 将此代码写入.h文件
@interface SelectPeopleViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITableView *tableView;
}
@property (nonatomic, retain) UITableView *tableView;
than add datasource and delegate of Uitableview to your File's Owner 而不是将数据源和Uitableview的委托添加到文件的所有者
write this code in .m file 将此代码写入.m文件
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
cell.textLabel.text = [NSString stringWithFormat:@"Cell Row #%d", [indexPath row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// open a alert with an OK and cancel button
NSString *alertString = [NSString stringWithFormat:@"Clicked on row #%d", [indexPath row]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertString message:@"" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
[alert show];
[alert release];
}
Yes you can add UITableView to an UIViewController's UIVIew if there is a necessity to add other controls to a view. 是的,如果需要向视图中添加其他控件,则可以将UITableView添加到UIViewController的UIVIew中。
If there is nothing other than a tableview in your viewcontroller's view then you can make it an subclass of UITableViewController as well. 如果在viewcontroller的视图中除了tableview外没有其他内容,则也可以使其成为UITableViewController的子类。
You can use tableView in your UIViewController 您可以在UIViewController中使用tableView
@interface MyViewController:UIViewController<UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableView *mytableview;
}
@property(nonatomic,retain) IBOutlet UITableView *mytableview;
Then in your viewController xib file create a tableview in view. 然后在您的viewController xib文件中在视图中创建一个tableview。 connect it to the mytableview in files owner.Also DONT FORGET to set delegate and data source to files owner. 将其连接到文件所有者中的mytableview。也不要设置将委托和数据源设置为文件所有者。
Later in your .m file create these functions and modify it according to your data. 稍后,在.m文件中创建这些功能并根据您的数据对其进行修改。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1; //how many sections in your tableView
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return [myArray count]; //how many rows you have
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = @"title";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog("%d.row selected",indexPath.row);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.