iOS开发:如何作为子类来创建和管理UITableView
已有 184 次阅读 2011-10-24 21:38 标签: UIViewController UIView UITableView iOS 在iPhone应用开发中个,常常碰到当用户点击按钮时,需要增加一个表格视图的操作。在开发实现上可以使用在UIViewController中增加一个UITableView视图的方案。
首先从UIViewController派生一个子类类(不需要使用nib文件),在子类loadView方法中装载UITableView,并且程序控制UITableView的大小和位置。代码如下:
// 使用程序创建视图层次关系
- (void)loadView
{
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
contentView.autoresizesSubviews = YES;
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
contentView.backgroundColor = [UIColor clearColor];
[self setView:contentView];
[contentView release];
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 440) style:UITableViewStylePlain];
[tableView setAutoresizesSubviews:YES];
[tableView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[tableView setDataSource:self];
[tableView setDelegate:self];
[[self view] addSubview:tableView];
}
头文件部分代码
@interface SelectPeopleViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITableView *tableView;
}
@property (nonatomic, retain) UITableView *tableView;