我有一个带自定义单元格的UITableView。由于我无法为标签添加约束,因此我尝试以编程方式执行此操作。现在一切正常,除非我开始滚动。不知何故,layoutsubviews方法被忽略或重置。
TableViewController.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SubjectCell *cell = (SubjectCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
cell.subjectLabel.text = [tentamenArray objectAtIndex:currentRow][@"SUMMARY"];
return cell;
}
SubjectCell.m
- (void) layoutSubviews {
[super layoutSubviews];
CGSize screen = [UIScreen mainScreen].bounds.size;
[_subjectLabel setFrame:CGRectMake(15, 15, screen.width - 70, 20)];
}
答案 0 :(得分:0)
使subjectLabel
可公开访问,并在ViewController本身内更改其框架。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SubjectCell *cell = (SubjectCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
cell.subjectLabel.text = [tentamenArray objectAtIndex:currentRow][@"SUMMARY"];
CGSize screen = [UIScreen mainScreen].bounds.size;
[cell.subjectLabel setFrame:CGRectMake(15, 15, screen.width - 70, 20)];
return cell;
}