iOS 7.1 UtableviewCell 内容与以下内容重叠 [英] iOS 7.1 UitableviewCell content overlaps with ones below

查看:18
本文介绍了iOS 7.1 UtableviewCell 内容与以下内容重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有代码,它可以在 iOS 7.0 上成功运行,但在 7.1 中没有.我有一个简单的表格视图,带有代码:

So I have code, which is sucessfully working on iOS 7.0 but not in 7.1. I have a simple tableview, with code:

- (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,但结果相同).

I have 1section with 10rows. With each row reused I delete all subviews from contentView(I tried alloc-init UITableViewCell, but came with the same results).

在 iOS 7.0 上,UILabel 仅显示在它所属的单元格中.但在 7.1 UILabel 继续显示在另一个单元格上.有趣的是,当我点击单元格时,它不再被其他人重叠,但直到我点击上面的单元格.我的问题是,如何让它在 7.1 设备上工作,比如在 7.0ones 上.

On iOS 7.0, UILabel is displayed only in cell it belongs to. But in 7.1 UILabel continues displaying over another cells. What is interresting, that when I click on cell, it stop being overlaping by anothers, but only till I click on cell above. My question is, how to make it work on 7.1devices like on 7.0ones.

我尝试了模拟器和设备,并查看了 iOS 7.1 API Diffs,但没有发现与此相关的任何内容.

I tried both simulator and device and I took a look at iOS 7.1 API Diffs, but found nothing related to this.

也许是自动布局的问题,我的 UILabel 高度可变,但我需要这样做.我想在 UILabel 中包含所有文本,但只显示 UILabel 的一部分,可以显示在单元格中,这是 7.0 中的默认行为,但 7.1 改变了这一点,我不知道为什么以及如何处理它.

Maybe it is issue of Auto Layout, that i have variable height of UILabel, but I need to do so. I want to have all text in UILabel, but display only part of UILabel, that can be displayed in a cell, which is default behavior in 7.0, but 7.1 changes this and I don't why why and how to deal with it.

这是带有详细说明的图片的保管箱文件夹:带图片的文件夹

This is dropbox folder for images with detail explanation: Folder with images

更新:我尝试了 tese 之类的东西,但没有任何效果.

Update: I tried things like tese, but nothing worked for me.

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;

推荐答案

问题与单元格的高度有关.它不会为您动态调整.

The issue has to do with the height of your cell. It isn't going to dynamically adjust that for you.

您可能会注意到,当您滚动时,上面的视图消失了,重叠的文本将随之消失.

You'll probably notice that as you scroll and the view above goes out of view the overlapping text will disappear with it.

如果您希望文本在某个高度剪辑,那么您需要设置行数,而不是将其设置为 0,因为这将让它永远持续下去.

If you are wanting your text to clip at a certain height, then you need to set the number of lines, rather than setting it to 0 since that will let it continue forever.

lineBreakMode不会生效,因为它没有停止.

The lineBreakMode won't take effect since it isn't stopped.

或者,您可以尝试在 contentView 上设置剪辑以确保所有子视图都留在里面.

Optionally you could try to set clipping on the contentView to make sure all subviews stay inside.

根据您想要的最终结果,您可以进行动态高度并根据内容进行更改.有很多与此相关的 SO 问题.

Depending on the end result you want, you could do dynamic heights and change based on the content. There are a bunch of SO questions related to doing this.

我必须自己尝试一下,但取而代之的是,这里有几个与裁剪 contentView 相关的链接:

I'd have to try it out myself, but in lieu of that, here are a couple links related to clipping the contentView:

  • stop the clipping of contentView - you'd actually want to do the opposite.
  • adding subview - looks similar to what you are trying to do.

看起来这样可行:

cell.clipsToBounds = YES;

这篇关于iOS 7.1 UtableviewCell 内容与以下内容重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆