使用Objective-C iOS以编程方式创建TableVIew [英] Creating A TableVIew Programmatically With Objective-C iOS

查看:106
本文介绍了使用Objective-C iOS以编程方式创建TableVIew的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to developing iOS applications and Objective C itself, so I have a probably very simple question.

目前我有以下方法调用工具栏按钮单击。该方法用于在帧变量 fr 中创建表视图。

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.

- (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中创建表视图,而是填充整个屏幕。

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.

再次,我是全新的,并且会感谢任何答案和任何建议。谢谢!

Again I'm totally new and would a appreciate any answers and any suggestions. Thanks!

推荐答案

当您设置 dataSource 委托 UITableView 的属性,这意味着,你必须为 dataSource

When you set dataSource and delegate properties for your UITableView, it means, you have to write at least this methods for 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

这篇关于使用Objective-C iOS以编程方式创建TableVIew的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆