gpt4 book ai didi

IOS - UITableViewCell 重叠 UITableViewCell

转载 作者:行者123 更新时间:2023-11-29 01:50:13 26 4
gpt4 key购买 nike

我在我的单元格中使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com