滚动时自定义UITableViewCell布局

时间:2014-12-01 12:31:16

标签: ios objective-c xcode scroll custom-cell

我有一个带自定义单元格的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)];
}

1 个答案:

答案 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;
}
相关问题