IOS - UITableViewCell 重叠 UITableViewCell

标签 ios objective-c uitableview autolayout

我在我的单元格中使用 AutoLayout,但是,当单元格内有大文本并且单元格展开时,单元格似乎与下面的单元格重叠。图像显示了这是如何重叠单元格的:

enter image description here

UITableViewCell:

        #import "PostTableViewCell.h"

        @implementation PostTableViewCell

        @synthesize main;
        @synthesize top;
        @synthesize center;
        @synthesize centerLeft;
        @synthesize centerRight;
        @synthesize bottom;
        @synthesize labelMessage;
        @synthesize labelCat;
        @synthesize labelSubCat;
        @synthesize labelCountry;
        @synthesize labelUser;
        @synthesize labelDate;
        @synthesize imageviewThreadImage;
        @synthesize split;


        - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
            self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
            if (self) {
            [self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
            self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

            CGRect screenBound = [[UIScreen mainScreen] bounds];
            CGSize screenSize = screenBound.size;
            CGFloat screenWidth = screenSize.width;
            //CGFloat screenHeight = screenSize.height;

            main = [UIView new];
            [self.contentView addSubview:main];
            main.translatesAutoresizingMaskIntoConstraints = NO;
            [main sizeToFit];
            main.backgroundColor = [UIColor whiteColor];

            top = [UIView new];
            [main addSubview:top];
            top.translatesAutoresizingMaskIntoConstraints = NO;
            [top sizeToFit];
            top.backgroundColor = [UIColor whiteColor];

            labelUser = [UILabel new];
            [top addSubview:labelUser];
            labelUser.translatesAutoresizingMaskIntoConstraints = NO;
            [labelUser sizeToFit];
            [labelUser setFont:[UIFont systemFontOfSize:10]];
            labelUser.textColor = [UIColor colorWithRed:(114.0/255.0) green:(114.0/255.0) blue:(114.0/255.0) alpha:1.0];

            labelDate = [UILabel new];
            [top addSubview:labelDate];
            labelDate.translatesAutoresizingMaskIntoConstraints = NO;
            [labelDate sizeToFit];
            [labelDate setFont:[UIFont systemFontOfSize:10]];
            labelDate.textColor = [UIColor colorWithRed:(114.0/255.0) green:(114.0/255.0) blue:(114.0/255.0) alpha:1.0];

            center = [UIView new];
            [main addSubview:center];
            center.translatesAutoresizingMaskIntoConstraints = NO;
            [center sizeToFit];
            center.backgroundColor = [UIColor whiteColor];


            imageviewThreadImage = [UIImageView new];
            [center addSubview:imageviewThreadImage];
            imageviewThreadImage.translatesAutoresizingMaskIntoConstraints = NO;
            imageviewThreadImage.backgroundColor = [UIColor colorWithRed:(207.0/255.0) green:(215.0/255.0) blue:(248.0/255.0) alpha:1.0];
            [imageviewThreadImage sizeToFit];

            labelMessage = [UILabel new];
            [center addSubview:labelMessage];
            labelMessage.translatesAutoresizingMaskIntoConstraints = NO;
            [labelMessage sizeToFit];
            [labelMessage setFont:[UIFont systemFontOfSize:12]];
            labelMessage.preferredMaxLayoutWidth = screenWidth - 10 - 36;
            labelMessage.lineBreakMode = NSLineBreakByWordWrapping;
            labelMessage.numberOfLines = 0;
            labelMessage.textColor = [UIColor lightGrayColor];
            //labelMessage.backgroundColor = [UIColor colorWithRed:(207.0/255.0) green:(215.0/255.0) blue:(248.0/255.0) alpha:1.0];
            //labelMessage.lineBreakMode = UILineBreakModeWordWrap;

            bottom = [UIView new];
            [main addSubview:bottom];
            bottom.translatesAutoresizingMaskIntoConstraints = NO;
            [bottom sizeToFit];

            labelCat = [UILabel new];
            [bottom addSubview:labelCat];
            labelCat.translatesAutoresizingMaskIntoConstraints = NO;
            [labelCat sizeToFit];
            [labelCat setFont:[UIFont systemFontOfSize:10]];
            labelCat.textColor = [UIColor colorWithRed:(58.0/255.0) green:(82.0/255.0) blue:(207.0/255.0) alpha:1.0];
            labelCat.lineBreakMode = NSLineBreakByWordWrapping;
            labelCat.numberOfLines = 0;

            labelSubCat = [UILabel new];
            [bottom addSubview:labelSubCat];
            labelSubCat.translatesAutoresizingMaskIntoConstraints = NO;
            [labelSubCat sizeToFit];
            [labelSubCat setFont:[UIFont systemFontOfSize:10]];
            labelSubCat.textColor = [UIColor colorWithRed:(58.0/255.0) green:(82.0/255.0) blue:(207.0/255.0) alpha:1.0];

            labelCountry = [UILabel new];
            [bottom addSubview:labelCountry];
            labelCountry.translatesAutoresizingMaskIntoConstraints = NO;
            [labelCountry sizeToFit];
            [labelCountry setFont:[UIFont systemFontOfSize:10]];
            labelCountry.textColor = [UIColor colorWithRed:(58.0/255.0) green:(82.0/255.0) blue:(207.0/255.0) alpha:1.0];

            split = [UIView new];
            [main addSubview:split];
            split.translatesAutoresizingMaskIntoConstraints = NO;
            [split sizeToFit];
            split.backgroundColor = [UIColor grayColor];


            }

            return self;
        }

        - (void)layoutSubviews
        {
            [super layoutSubviews];
            // Make sure the contentView does a layout pass here so that its subviews have their frames set, which we
            // need to use to set the preferredMaxLayoutWidth below.
            [self.contentView setNeedsLayout];
            [self.contentView layoutIfNeeded];

            // Set the preferredMaxLayoutWidth of the mutli-line bodyLabel based on the evaluated width of the label's frame,
            // as this will allow the text to wrap correctly, and as a result allow the label to take on the correct height.
            self.labelMessage.preferredMaxLayoutWidth = CGRectGetWidth(self.labelMessage.frame);
        }

        - (void)updateConstraints {
            [super updateConstraints];

            if (self.didSetupConstraints) return;

            NSDictionary *viewsDictionary7 = @{@"main":main};
            NSArray *constraint_H37 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[main]|"
                                              options:0
                                              metrics:nil
                                            views:viewsDictionary7];

            NSArray *constraint_V37 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[main]|"
                                              options:0
                                              metrics:nil
                                            views:viewsDictionary7];
            [self.contentView addConstraints:constraint_H37];
            [self.contentView addConstraints:constraint_V37];

            //NSString *text = labelMessage.text;

            NSDictionary *viewsDictionary3 = @{@"top":top,@"center":center,@"bottom":bottom,@"split":split};
            NSArray *constraint_H3 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[top]-5-[center(>=40)]-5-[bottom]-10-[split(1)]|"
                                             options:0
                                             metrics:nil
                                               views:viewsDictionary3];

            NSArray *constraint_H33cvcv4545fdd = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[split]|"
                                                 options:0
                                                 metrics:nil
                                                   views:viewsDictionary3];

            NSArray *constraint_H33 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[top]|"
                                              options:0
                                              metrics:nil
                                            views:viewsDictionary3];

            NSArray *constraint_H333 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[center]|"
                                               options:0
                                               metrics:nil
                                             views:viewsDictionary3];

            NSArray *constraint_H3335657 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[bottom]|"
                                               options:0
                                               metrics:nil
                                                 views:viewsDictionary3];

            NSDictionary *viewsDictionary4 = @{@"labelUser":labelUser,@"labelDate":labelDate};
            NSArray *constraint_H4 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[labelUser]|"
                                             options:0
                                             metrics:nil
                                               views:viewsDictionary4];

            NSDictionary *viewsDictionary45 = @{@"labelDate":labelDate};
            NSArray *constraint_H4555 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[labelDate]|"
                                            options:0
                                            metrics:nil
                                              views:viewsDictionary45];

            NSArray *constraint_H44 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[labelUser]-20-[labelDate]-5-|"
                                              options:0
                                              metrics:nil
                                            views:viewsDictionary4];

            NSDictionary *viewsDictionary48 = @{@"labelMessage":labelMessage,@"imageviewThreadImage":imageviewThreadImage};



            NSArray *constraint_H48 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[labelMessage]-5-|"
                                              options:0
                                              metrics:nil
                                            views:viewsDictionary48];

            NSArray *constraint_H48342 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[imageviewThreadImage(36)]"
                                             options:0
                                             metrics:nil
                                               views:viewsDictionary48];



            NSArray *constraint_H448345fgdfg = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[imageviewThreadImage(36)]"
                                               options:0
                                               metrics:nil
                                                 views:viewsDictionary48];

            CGRect screenBound = [[UIScreen mainScreen] bounds];
            CGSize screenSize = screenBound.size;
            CGFloat screenWidth = screenSize.width;
            //CGFloat screenHeight = screenSize.height;


            NSArray *constraint_H448345fgdfgdfdf454 = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-46-[labelMessage(%f)]-10-|",screenWidth - 51]
                                                   options:0
                                                   metrics:nil
                                                 views:viewsDictionary48];




            /**NSArray *constraint_H448 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[labelMessage]-5-|"
                                               options:0
                                               metrics:nil
             views:viewsDictionary48];**/

            NSDictionary *viewsDictionary488 = @{@"labelCat":labelCat,@"labelCountry":labelCountry,@"labelSubCat":labelSubCat};
            NSArray *constraint_H488 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[labelCat]|"
                                               options:0
                                               metrics:nil
                                             views:viewsDictionary488];
            NSArray *constraint_H48898 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[labelCountry]|"
                                             options:0
                                             metrics:nil
                                               views:viewsDictionary488];
            NSArray *constraint_H48898fgf54 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[labelSubCat]|"
                                                  options:0
                                                  metrics:nil
                                                views:viewsDictionary488];

            NSArray *constraint_H4488 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[labelCat]-10-[labelSubCat]-10-[labelCountry]"
                                            options:0
                                            metrics:nil
                                              views:viewsDictionary488];



            [self.contentView addConstraints:constraint_H3];
            [self.contentView addConstraints:constraint_H33];
            [self.contentView addConstraints:constraint_H333];
            [self.contentView addConstraints:constraint_H4];
            [self.contentView addConstraints:constraint_H44];
            [self.contentView addConstraints:constraint_H48];
            //[self.contentView addConstraints:constraint_H448];
            [self.contentView addConstraints:constraint_H4555];
            [self.contentView addConstraints:constraint_H3335657];
            [self.contentView addConstraints:constraint_H488];
            [self.contentView addConstraints:constraint_H48342];
            [self.contentView addConstraints:constraint_H4488];
            [self.contentView addConstraints:constraint_H448345fgdfg];
            [self.contentView addConstraints:constraint_H48898];
            [self.contentView addConstraints:constraint_H48898fgf54];
            [self.contentView addConstraints:constraint_H33cvcv4545fdd];
            [self.contentView addConstraints:constraint_H448345fgdfgdfdf454];



            self.didSetupConstraints = YES;

        }



        - (void)awakeFromNib {
            // Initialization code
        }

        - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
            [super setSelected:selected animated:animated];

            // Configure the view for the selected state
        }

        @end

如何重复使用我的细胞:

CellForRowIndexPath -

if(tableView == tableViewPost){


    Post *t = (Post*)[tmpArray2 objectAtIndex:indexPath.row];

            static NSString *simpleTableIdentifier = @"SimpleTableItem46";

            PostTableViewCell *cell = (PostTableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
            //cell.layer.shouldRasterize = YES;
            //cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
            if (cell == nil) {
            cell = [[PostTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
            }


            cell.labelMessage.text = t.message;
            cell.labelCat.text = t.cat;
            cell.labelSubCat.text = t.subcat;
            cell.labelCountry.text = t.country;
            cell.labelUser.text = [NSString stringWithFormat:@"%@", t.user];
            cell.labelDate.text = [NSString stringWithFormat:@"%@", t.date];


            [cell.contentView setNeedsUpdateConstraints];
            [cell.contentView updateConstraintsIfNeeded];
            [cell.contentView setNeedsLayout];
            [cell.contentView layoutIfNeeded];
            return cell;

        }

行索引路径高度 -

if(tableView == tableViewPost){



        Post *t = (Post*)[tmpArray2 objectAtIndex:indexPath.row];

            PostTableViewCell *cell = (PostTableViewCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];

            [cell.contentView setNeedsUpdateConstraints];
            [cell.contentView updateConstraintsIfNeeded];
            [cell.contentView setNeedsLayout];
            [cell.contentView layoutIfNeeded];


            CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
            //CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];



            return height;
        }

最佳答案

如果您使用的是 iOS 8 或更高版本,您可以尝试

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 70.0

关于IOS - UITableViewCell 重叠 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31493887/

相关文章:

ios - 将 UITableViewController 插入其他 UIView

ios - 使用 UIEdgedInset 的自定义 UISlider 渲染效果不佳

ios - Objective-C/iPhone 'Hello World' 应用程序的显示不正确

xcode - 用于使用最新 sdk 的 xcodebuild 参数。

ios - 当我尝试使用它的响应时,异步 http 调用还没有准备好

ios - mkmap查看用户位置

ios - 已删除默认 png 现在构建失败

iphone - UITableViewController 推送时没有导航栏

ios - 应用内购买,始终无效的产品 ID

uitableview - Swift:不正确地使用 UIRefreshControl - 无法识别的选择器