所以我有代码,它在 iOS 7.0 上成功运行,但在 7.1 上没有。我有一个简单的表格视图,带有代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview];
}
UILabel *label = [[UILabel alloc] init];
label.text = [NSString string];
for (NSInteger i = 0; i < 20; i++) {
label.text = [label.text stringByAppendingString:@"label String "];
}
label.translatesAutoresizingMaskIntoConstraints = NO;
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWorldWrapping;
//label.lineBreakMode = NSLineBreakByTruncatingTail; //I have tried this too
[cell.contentView addSubview:label];
NSDictionary *dict = NSDictionaryOfVariableBindings(label);
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-8-[label]-8-|" options:0 metrics:nil views:dict]];
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[label]" options:0 metrics:nil views:dict]];
if (indexPath.row == 0) {
label.textColor = [UIColor colorWithRed:1.0 green:0 blue:0 alpha:1.0];
}
else if (indexPath.row == 1) {
label.textColor = [UIColor colorWithRed:0 green:1.0 blue:0 alpha:1.0];
}
else if (indexPath.row == 2) {
label.textColor = [UIColor colorWithRed:0 green:0 blue:1.0 alpha:1.0];
}
else {
label.textColor = [UIColor colorWithWhite:0.3 alpha:1.0];
}
cell.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1.0];
return cell;
}
我有 1 节,有 10 行。重用每一行后,我从 contentView 中删除了所有子视图(我尝试了 alloc-init UITableViewCell,但结果相同)。
在 iOS 7.0 上,UILabel 仅显示在它所属的单元格中。但在 7.1 UILabel 继续显示在另一个单元格上。有趣的是,当我点击单元格时,它不再被其他人重叠,但直到我点击上面的单元格。我的问题是,如何让它在 7.1 设备上工作,比如在 7.0ones 上。
我尝试了模拟器和设备,并查看了 iOS 7.1 API Diffs,但没有发现与此相关的任何内容。
也许是自动布局的问题,我的 UILabel 高度可变,但我需要这样做。我想在 UILabel 中包含所有文本,但只显示 UILabel 的一部分,可以显示在单元格中,这是 7.0 中的默认行为,但 7.1 改变了这一点,我不知道为什么以及如何处理它。
这是带有详细说明的图像的保管箱文件夹:带图像的文件夹
更新:我尝试了像 tese 这样的东西,但对我没有任何帮助。
cell.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 70);
cell.contentView.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 70);
cell.opaque = NO;
cell.contentView.opaque = NO;
cell.clearsContextBeforeDrawing = NO;
cell.contentView.clearsContextBeforeDrawing = NO;
cell.clipsToBounds = NO;
cell.contentView.clipsToBounds = NO;