【发布时间】:2019-04-15 16:44:38
【问题描述】:
我尝试以编程方式将 UITableView 添加到滚动视图中。我已经设置了锚点和数据源,委托。你可以在代码中看到。但我不能。我放了一个断点,一切都运行了。但我在滚动视图中看不到。
UITableView *tableView = [[UITableView alloc] init];
tableView.rowHeight = 130;
tableView.translatesAutoresizingMaskIntoConstraints = false;
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = [UIColor clearColor];
[self.scrollView addSubview:tableView];
[tableView.leftAnchor constraintEqualToAnchor:self.scrollView.leftAnchor constant:8].active = YES;
[tableView.rightAnchor constraintEqualToAnchor:self.scrollView.rightAnchor constant:8].active = YES;
[tableView.topAnchor constraintEqualToAnchor:self.viewShareBasketExtra.topAnchor constant:8].active = YES;
[tableView reloadData];
此代码有效:
UILabel *lblPrice = [[UILabel alloc] init];
[lblPrice setFont:[UIFont fontWithName:@"HelveticaNeue" size:14]];
lblPrice.translatesAutoresizingMaskIntoConstraints = false;
[lblPrice setTextAlignment:NSTextAlignmentCenter];
lblPrice.text = [NSString stringWithFormat:@"%.2f TL",[_productModel Price]];
[self.priceView addSubview:lblPrice];
[lblPrice.leftAnchor constraintEqualToAnchor:self.priceView.leftAnchor constant:8].active = YES;
[lblPrice.rightAnchor constraintEqualToAnchor:self.priceView.rightAnchor constant:8].active = YES;
[lblPrice.centerXAnchor constraintEqualToAnchor:self.priceView.centerXAnchor].active = YES;
[lblPrice.centerYAnchor constraintEqualToAnchor:self.priceView.centerYAnchor].active = YES;
[self getComments];
但是这些代码不起作用:
UITableView *tableView = [[UITableView alloc]init];
tableView.rowHeight = 130;
tableView.translatesAutoresizingMaskIntoConstraints = false;
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = [UIColor clearColor];
[self.scrollView addSubview:tableView];
[tableView.leftAnchor constraintEqualToAnchor:self.scrollView.leftAnchor constant:8].active = YES;
[tableView.rightAnchor constraintEqualToAnchor:self.scrollView.rightAnchor constant:8].active = YES;
[tableView.topAnchor constraintEqualToAnchor:self.webView.topAnchor constant:8].active = YES;
我的委托方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(commentsArray!=nil){
return [commentsArray count];
}else
{
return 0;
}
}
我正在使用自定义表格单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identity = @"CommentTableViewCell";
CommentTableViewCell *cell = (CommentTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identity];
if(cell == nil){
@try {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CommentTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
@catch (NSException *exception) {
NSLog(@"Err:%@", exception.reason);
}
@finally {
}
}
【问题讨论】:
-
是否调用了委托/数据源方法?你的 UITableView 的高度是多少?它应该是什么?
-
我希望高度是动态的,我正在使用自定义 uitablecell。
-
'- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(cmetsArray!=nil){ return [cmetsArray count]; } 其他 { 返回 0; } }'
-
如果同时添加底部锚点会怎样?表格视图本身是一个滚动视图,将滚动其内容,但没有高度,它将不可见。这可能是您遇到的问题。
标签: ios objective-c uicontrol