[英]Creating A TableVIew Programmatically With Objective-C iOS
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 .该方法旨在在框架变量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!谢谢!
When you set dataSource
and delegate
properties for your UITableView
, it means, you have to write at least this methods for dataSource
:当您为UITableView
设置dataSource
和delegate
属性时,这意味着您必须至少为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
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 //-- 表格 header 高度(如果需要)
- (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
}
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.这将正确尊重您设置的框架。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.