使用Objective-C iOS以编程方式创buildTableVIew

我是新开发的iOS应用程序和Objective C本身,所以我有一个可能非常简单的问题。

目前我有下面的方法,从工具栏button点击调用。 该方法devise用于在框架variablesfr中创build一个表视图。

- (IBAction)addGolfer:(id)sender { CGRect fr = CGRectMake(101, 45, 100, 416); UITableView *tabrleView = [[UITableView alloc] initWithFrame:fr style:UITableViewStylePlain]; tabrleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; tabrleView.delegate = self; tabrleView.dataSource = self; [tabrleView reloadData]; self.view = tableView; } 

调用这个方法的结果并不是我所期望的。 表格视图不是在“fr”框架中创build表格视图,而是填充整个屏幕。

再次,我是全新的,并会欣赏任何答案和任何build议。 谢谢!

当你为你的UITableView设置dataSourcedelegate属性时,就意味着你必须至less为dataSource写这个方法:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 

如果你不这样做,它会崩溃。 总结你会得到这个( 这段代码可能包含语法或逻辑错误 – 我写在记事本):

 @interface YourViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> { UITableView *firstTableView; UITableView *secondTableView; } @end 

//

 @implementation YourViewController #pragma mark - Objects Processing - (void)addGolfer:(UIBarButtonItem *)sender { if (secondTableView) { [secondTableView removeFromSuperView]; secondTableView = nil; } secondTableView = [[UITableView alloc] initWithFrame:CGRectMake(101, 45, 100, 416)]; secondTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; secondTableView.delegate = self; tabrleView.dataSource = self; [self.view addSubview:secondTableView]; } #pragma mark - TableView DataSource Implementation - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == firstTableView) { // your tableView you had before return 20; // or other number, that you want } else if (tableView == secondTableView) { return 15; // or other number, that you want } } - (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]; cell.backgroundView = [[UIView alloc] init]; [cell.backgroundView setBackgroundColor:[UIColor clearColor]]; [[[cell contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; if (tableView == firstTableView) { // your tableView you had before // ... } else if (tableView == secondTableView) { cell.titleLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row + 1]; } return cell; } @end 

第1步:添加委托UITableViewDataSource,UITableViewDelegate

 @interface viewController: UIViewController<UITableViewDataSource,UITableViewDelegate> { UITableView *tableView; } 

第2步:

 -(void)viewDidLoad { tableView=[[UITableView alloc]init]; tableView.frame = CGRectMake(10,30,320,400); tableView.dataSource=self; tableView.delegate=self; tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; [tableView reloadData]; [self.view addSubview:tableView]; } 

第3步: tableview的属性(行和列)

// – 表中没有行

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } 

// – 表头高度,如果需要的话

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 50; } 

// – 将数据分配给单元格

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath] ; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.textLabel.text=[your_array objectAtIndex:indexPath.row]; ***(or)*** cell.textLabel.text = @"Hello"; return cell; } 

// – 触摸单元格时的操作

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Your custom operation } 

而不是设置UIViewController的视图,添加tableView作为子视图。

代替:

 self.view = tableView; 

做这个:

 [self.view addSubview:tableView]; 

这将正确地尊重你设置的框架。