我有以下代码以编程方式创建带有表的视图
- (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
,所以我想知道dataSource
的delegate
和tableDeviceList
属性的分配是否存在问题。 最佳答案
您需要在您的课程中实现dataSource
的delegate
和UITableView
方法,
- (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;
}
关于ios - 如何以编程方式创建UITableView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53784587/