【发布时间】: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的类型,所以我想知道tableDeviceList的dataSource和delegate属性的赋值会不会有问题。
【问题讨论】:
-
注意编译器警告。
tableDeviceList.dataSource = self;可能显示 self 不符合数据源协议的警告。将 VC 声明为符合(参见此处的“符合”部分:developer.apple.com/library/archive/documentation/Cocoa/…)。这也会产生一个警告,即符合标准的类必须实现所需的方法。请参阅下面的@Bappaditya,了解下一步该做什么。注意警告...其中许多预测运行时崩溃。
标签: ios objective-c uitableview