iOS 7.1 UitableviewCell 内容与以下内容重叠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22358831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:42:48  来源:igfitidea点击:

iOS 7.1 UitableviewCell content overlaps with ones below

iosuitableviewuilabelautolayoutios7.1

提问by Matej Kosiarcik

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

所以我有代码,它可以在 iOS 7.0 上成功运行,但在 7.1 中却没有。我有一个简单的tableview,代码如下:

- (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;
}

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).

我有 1 个 10 行的部分。重用每一行后,我从 contentView 中删除所有子视图(我尝试了 alloc-init UITableViewCell,但结果相同)。

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.0 上,UILabel 只显示在它所属的单元格中。但是在 7.1 UILabel 中继续显示在另一个单元格上。有趣的是,当我单击单元格时,它不再与其他单元格重叠,但直到我单击上面的单元格为止。我的问题是,如何让它在 7.1 设备上像 7.0ones 一样工作。

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

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

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.

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

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

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

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

更新:我尝试了像 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;

回答by RyanJM

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.

您可能会注意到,当您滚动并且上面的视图从视图中消失时,重叠的文本将随之消失。

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.

如果您希望文本以特定高度剪切,则需要设置行数,而不是将其设置为 0,因为这将让它永远持续下去。

The lineBreakModewon't take effect since it isn't stopped.

lineBreakMode因为它不会停止将不会生效。

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

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

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.

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

Update - clipping the contentView

更新 - 剪辑 contentView

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

我必须自己尝试一下,但作为替代,这里有几个与剪切 contentView 相关的链接:

Looks like this works:

看起来像这样:

cell.clipsToBounds = YES;

回答by Gaurav Singla

Here is the Perfect solution of Overlapping content in Cells.

这是单元格中重叠内容的完美解决方案。

Just use below code in cellForRowAtIndexPath after allocating cell and before adding subviews.

只需在分配单元格后和添加子视图之前在 cellForRowAtIndexPath 中使用以下代码。

for (id object in cell.contentView.subviews)
{
    [object removeFromSuperview];
}  

Actually the overlapping is occurring because whenever you scroll the tableview its allocating your added view again and again. So above code will solve your problem by removing the existing views from cell's contentView.

实际上,重叠正在发生,因为每当您滚动 tableview 时,它都会一次又一次地分配您添加的视图。因此,上面的代码将通过从单元格的 contentView 中删除现有视图来解决您的问题。

Now You can see the memory debug session after applying above code, your memory is stable this time.

现在您可以看到应用上述代码后的内存调试会话,这次您的内存是稳定的。

Hope it'll help you.

希望它会帮助你。

Thanks !

谢谢 !

回答by Emastmagy MastMagy

@Gaurav your answer should be the accepted answer. Thanks!

@Gaurav 您的答案应该是公认的答案。谢谢!

for object in cell.contentView.subviews
            {
                object.removeFromSuperview();
            }

回答by GraehamF

had similar behavior in iOS 8, using storyboard / IB.

在 iOS 8 中有类似的行为,使用 storyboard / IB。

fix was to add a Bottom Space to: Superviewconstraint from the bottom-most view to bottom of the prototype cell's Content View. The other views and constraints were all anchored from the top.

修复方法是Bottom Space to: Superview从最底部的视图到原型单元的内容视图的底部添加一个约束。其他视图和约束都从顶部锚定。

回答by Samitha K

This is problem with recreating cell contents. Try with following code segment.

这是重新创建单元格内容的问题。尝试使用以下代码段。

for(UIView *view in cell.contentView.subviews){  
        if ([view isKindOfClass:[UIView class]]) {  
            [view removeFromSuperview];   
        }
    }

回答by TooManyEduardos

Are you using storyboards? If so, select the table view controller in storyboards and uncheck the "Under bottom bars" You can also do this programmatically.

你在使用故事板吗?如果是这样,请在情节提要中选择表格视图控制器并取消选中“在底部栏下”您也可以以编程方式执行此操作。

If your TVC inherits from a nav view controller or a tab view controller, you may need to uncheck this layout option on the parent view instead

如果您的 TVC 继承自导航视图控制器或选项卡视图控制器,您可能需要在父视图上取消选中此布局选项

回答by Divyansh Jain

Re-checking all 4 constraints helps. While working with table view or collection view it's necessary to apply all four constraints (leading, trailing, top, bottom.)

重新检查所有 4 个约束会有所帮助。在使用表视图或集合视图时,有必要应用所有四个约束(前导、尾随、顶部、底部)。