【问题标题】:How to create UITableView programmatically如何以编程方式创建 UITableView
【发布时间】:2018-12-14 17:47:26
【问题描述】:

我有以下代码以编程方式创建带有表格的视图

- (void) createView:(UIViewController*)viewController{
    _thisViewController = viewController;
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;

    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"Title";
    titleLabel.frame = CGRectMake(75, 20, 200, 50);
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.font = [titleLabel.font fontWithSize: 24];

    UILabel *subtitleLabel = [[UILabel alloc] init];
    subtitleLabel.text = @"Subtitle";
    subtitleLabel.frame = CGRectMake(75, 50, 400, 50);
    subtitleLabel.textColor = [UIColor blackColor];
    subtitleLabel.font = [subtitleLabel.font fontWithSize: 20];

    UILabel *labelStatus = [[UILabel alloc] init];
    labelStatus.text = @"";
    labelStatus.frame = CGRectMake(75, screenHeight - 290, 500, 50);
    labelStatus.textColor = [UIColor blueColor];
    labelStatus.font = [labelStatus.font fontWithSize: 20];

    UITableView *tableDeviceList = [[UITableView alloc] init];
    tableDeviceList.frame = CGRectMake(75, 100, screenWidth -250, screenHeight - 400);
    tableDeviceList.dataSource = self;
    tableDeviceList.delegate = self;

    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [cancelButton setTitle:@"Cancelar" forState:UIControlStateNormal];
    [cancelButton sizeToFit];

    cancelButton.frame = CGRectMake((screenWidth/2)-100, screenHeight - 250, 100, 50);
    cancelButton.backgroundColor = UIColorFromRGB(0x066D8B);
    cancelButton.layer.cornerRadius = 20;
    [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    UIViewController *viewControllerModal = [[UIViewController alloc] init];

    UIView *nooView = [[UIView alloc] initWithFrame:CGRectMake(50, 80, screenWidth - 100, screenHeight - 160)];
    nooView.backgroundColor = UIColorFromRGB(0xE8E8E8);
    nooView.layer.cornerRadius = 20;

    [nooView addSubview:titleLabel];
    [nooView addSubview:subtitleLabel];
    [nooView addSubview:tableDeviceList];
    [nooView addSubview:labelStatus];
    [nooView addSubview:cancelButton];

    [viewControllerModal.view addSubview:nooView];

    [viewController presentViewController: viewControllerModal animated:YES completion:nil];
}

这给我带来了tableView:numberOfRowsInSection: 的错误,所以我想知道是否有办法向以这种方式创建的UITableView 添加函数(我正在以编程方式创建我的UITableView)。

另外,我的类是NSObject的类型,所以我想知道tableDeviceListdataSourcedelegate属性的赋值会不会有问题。

【问题讨论】:

  • 注意编译器警告。 tableDeviceList.dataSource = self; 可能显示 self 不符合数据源协议的警告。将 VC 声明为符合(参见此处的“符合”部分:developer.apple.com/library/archive/documentation/Cocoa/…)。这也会产生一个警告,即符合标准的类必须实现所需的方法。请参阅下面的@Bappaditya,了解下一步该做什么。注意警告...其中许多预测运行时崩溃。

标签: ios objective-c uitableview


【解决方案1】:

你需要在你的类中实现UITableViewdataSourcedelegate方法,

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1; // number of sections
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10; // should be your array count
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellId = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    cell.textlabel.text = @"test";
    return cell;
}

【讨论】:

  • 值得指出的是 numberOfRows 和 cellForRow 应该访问相同的数据源数据,即在 cellForRow 中:id dataForThisCell = self.theDatasourceArray[indexPath.row]; 和 numberOfRows 说:return self.theDatasourceArray.count
  • 对不起,我是ios开发的新手,所以我很困惑。我已经阅读了使用 UITableView 需要其中一些方法,但是该代码中数据源和委托的使用在哪里?
  • @amlibtest,您的视图控制器成为数据源,方法是声明它是一个,将表视图数据源设置为 self,并实现这些方法。通过这样做,你对表格视图说:“你做了所有表格的事情,比如滚动和重用单元格,但我会告诉你有多少个单元格以及如何配置它们。只需通过数据源协议问我"
【解决方案2】:

首先,您应该设置以下内容:

    UITableView(your tableview name).delegate = self;
    UITableView(your tableview name).dataSource = self;

其次,你需要实现委托方法和数据源方法 如:

【讨论】:

    猜你喜欢
    • 2013-03-28
    • 1970-01-01
    • 2011-06-08
    • 2015-02-25
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多