使用 Objective-C iOS 以编程方式创建 TableVIew
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10003034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating A TableVIew Programmatically With Objective-C iOS
提问by The Man
I'm new to developing iOS applications and Objective C itself, so I have a probably very simple question.
我是开发 iOS 应用程序和 Objective C 本身的新手,所以我有一个可能非常简单的问题。
Currently I have the following method that is called from a ToolBar Button click. The method is designed to create a table view in the frame variable fr.
目前我有以下从 ToolBar Button 单击调用的方法。该方法旨在在框架变量fr 中创建表视图。
- (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;
}
The result of calling this method is not what I expect. Instead of creating the Table View in the frame "fr", the table view fills the entire screen.
调用这个方法的结果不是我所期望的。不是在框架“fr”中创建表格视图,表格视图填充整个屏幕。
Again I'm totally new and would a appreciate any answers and any suggestions. Thanks!
再次,我是全新的,将不胜感激任何答案和任何建议。谢谢!
回答by demon9733
When you set dataSource
and delegate
properties for your UITableView
, it means, you have to write at least this methods for dataSource
:
当您为您的设置dataSource
和delegate
属性时UITableView
,这意味着您必须至少为 编写以下方法dataSource
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
If you wouldn't do this, it will be crash. Summary you'll get this (this code may contain syntax or logic errors- I wrote it in notepad):
如果你不这样做,它就会崩溃。总结你会得到这个(这段代码可能包含语法或逻辑错误——我用记事本写的):
@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
回答by Rajesh Loganathan
Step 1:Add delegate UITableViewDataSource,UITableViewDelegate
第 1 步:添加委托UITableViewDataSource,UITableViewDelegate
@interface viewController: UIViewController<UITableViewDataSource,UITableViewDelegate>
{
UITableView *tableView;
}
Step 2:
第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];
}
Step 3:Properties for tableview (rows & column)
第 3 步:tableview 的属性(行和列)
//-- For no of rows in table
//-- 对于表中的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//-- Table header height if needed
//-- 如果需要,表格标题高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}
//-- Assign data to cells
//- 将数据分配给单元格
- (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;
}
//-- Operation when touch cells
//-- 触摸单元格时的操作
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Your custom operation
}
回答by DHamrick
Instead of setting the view of the UIViewController
, add the tableView as a subview.
不是设置 的视图,而是UIViewController
将 tableView 添加为子视图。
Instead of:
代替:
self.view = tableView;
Do this:
做这个:
[self.view addSubview:tableView];
This will properly respect the frame that you set.
这将正确尊重您设置的框架。