我正在绘制一个相当简单的自定义UITableViewCell - 左侧的缩略图,右侧的名称和链接标签。我希望我的名字和链接标签是单行的,如果文本超出单元格的宽度,则在尾部截断。我很确定我正在添加我的约束,但我的名字和链接标签不会兑现它们。
这是运行iOS 8的iPhone 4S上的单元格:
在我的代码中,我将名称和链接标签的10点尾随约束添加到超级视图(单元格contentView
)但是,请注意即使在横向,iOS也不会# 39;似乎尊重约束。
有人能告诉我我做错了什么吗?这是我整个单元格的代码。我正在以编程方式创建视图,并且我在表格视图单元格的初始化程序中具有Autolayout约束。
#import "PopularWikiCell.h"
// Models
#import "Wiki.h"
// Pods
#import "UIImageView+AFNetworking.h"
// Constants
static const CGFloat kPadding = 10;
static const CGFloat kImageSize = 100;
const CGFloat kPopularWikiCellHeight = kImageSize + kPadding * 2;
@interface PopularWikiCell ()
@property (nonatomic, strong) UIImageView *thumbnailView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *linkLabel;
@end
@implementation PopularWikiCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// Initialise the subviews.
_thumbnailView = [[UIImageView alloc] init];
_thumbnailView.contentMode = UIViewContentModeScaleAspectFill;
_thumbnailView.clipsToBounds = YES;
_nameLabel = [[UILabel alloc] init];
_nameLabel.backgroundColor = [UIColor blueColor];
_nameLabel.numberOfLines = 1;
_nameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
_nameLabel.text = @"Name";
_linkLabel = [[UILabel alloc] init];
_linkLabel.backgroundColor = [UIColor redColor];
_linkLabel.text = @"Link";
_linkLabel.numberOfLines = 1;
_linkLabel.lineBreakMode = NSLineBreakByTruncatingTail;
// Add the subviews.
[self.contentView addSubview:_thumbnailView];
[self.contentView addSubview:_nameLabel];
[self.contentView addSubview:_linkLabel];
_thumbnailView.translatesAutoresizingMaskIntoConstraints = NO;
_nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
_linkLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
// Add the layout constraints.
NSDictionary *metrics = @{@"kPadding" : @(kPadding),
@"kImageSize" : @(kImageSize)};
NSDictionary *views = @{@"thumbnailView" : _thumbnailView,
@"nameLabel" : _nameLabel,
@"linkLabel" : _linkLabel};
NSArray *constraints = @[@"H:|-kPadding-[thumbnailView(kImageSize)]-kPadding-[nameLabel]-kPadding-|",
@"H:[thumbnailView]-kPadding-[linkLabel]-kPadding-|",
@"V:|-kPadding-[nameLabel]-0-[linkLabel]",
@"V:|-kPadding-[thumbnailView(kImageSize)]"];
[self.contentView addConstraintsFromVisualFormatStrings:constraints
options:0
metrics:metrics
views:views];
}
return self;
}
- (void)setWiki:(Wiki *)wiki
{
_wiki = wiki;
self.thumbnailView.image = nil;
self.thumbnailView.backgroundColor = [UIColor darkGrayColor];
__weak typeof(self) weakSelf = self;
[self.thumbnailView setImageWithURLRequest:[NSURLRequest requestWithURL:wiki.thumbnailURL]
placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
__strong typeof(self) strongSelf = weakSelf;
strongSelf.thumbnailView.image = image;
strongSelf.thumbnailView.backgroundColor = [UIColor whiteColor];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
}];
self.nameLabel.text = wiki.name;
self.linkLabel.text = wiki.URL;
[self.contentView setNeedsLayout];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Selection removes all background colors so re-set it.
[self resetSubviewBackgroundColors];
[self.contentView setNeedsLayout];
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
[super setHighlighted:highlighted animated:animated];
// Highlighing removes all background colors so re-set it.
[self resetSubviewBackgroundColors];
[self.contentView setNeedsLayout];
}
- (void)resetSubviewBackgroundColors
{
if (self.thumbnailView.image) {
self.thumbnailView.backgroundColor = [UIColor whiteColor];
} else {
self.thumbnailView.backgroundColor = [UIColor darkGrayColor];
}
}
@end
答案 0 :(得分:9)
问题隐藏在
中self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
删除此代码可以解决您的问题。
如果你不希望 traslate Autoresizing Mask Into Constraints - 设置你对cell.contentView的理想约束。
答案 1 :(得分:1)
在你的cellForRow:方法中确保你正在调用:
cell = [tableView dequeueReusableCellWithReuseIdentifier:@"" forIndexPath:(IndexPath *)
调用它会返回一个正确大小的单元格(对于autolayout很重要)
如果这不能解决问题,请在您的单元格内执行:
-(void)layoutSubview{
[super layoutSubviews];
[self layoutIfNeeded];
}
答案 2 :(得分:0)
首先我强烈建议使用自动布局,它解决了很多尺寸问题和许多计算
但是,如果你没有设置约束,那么你必须计算字符串的长度以适合你想要的标签的宽度
NSString *string = @"this is a very long string to fit into a UILabel";
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 150, 20)];
UIFont *font = [UIFont fontWithName:@"Helvetica Neue" size:15.0];
label.font = font;
label.text = string;
CGSize stringSize = [string sizeWithAttributes:@{NSFontAttributeName : font}];
// check if the string is wider then the label
if (stringSize.width > label.bounds.size.width) {
NSString *newString = [string substringWithRange:NSMakeRange(0, string.length/2)]; <---- i just divided the string into two -----
// check if the newString fits the label
// if not the divide again
label.text = newString;
}else{
label.text = string;
}
[self.view addSubview:label];
像我在代码中写的那样,我只是将字符串分成两个字符串。如果你有一个很长的字符串,这可能不起作用,所以你需要继续检查字符串是否符合标签。
您可以使用:
[NSString stringWithFormat:@"%@...", [string substringToIndex:20]];
添加&#34; ...&#34;在字符串的末尾
答案 3 :(得分:0)
您好,您可以在自定义UITableViewCell中尝试此操作,使用以下内容设置内容视图的框架或标签或任何元素: -
声明: -
GRect usableSpace;
CGFloat usableWidth, usableHeight;
在实施中: -
usableSpace = [[UIScreen mainScreen] applicationFrame];
usableWidth = usableSpace.size.width;
usableHeight = usableSpace.size.height;
然后使用这些宽度和高度设置框架
答案 4 :(得分:0)
我无法加载您的图片,因此我不确定您遇到了什么问题。
我看到这段代码,有拼写错误吗?
NSArray *constraints =
@[
@"H:|-kPadding-[thumbnailView(kImageSize)]-kPadding-[nameLabel]-kPadding-|",
@"H:[thumbnailView]-kPadding-[linkLabel]-kPadding-|", //this line you forgot the left edge
@"V:|-kPadding-[nameLabel]-0-[linkLabel]",
@"V:|-kPadding-[thumbnailView(kImageSize)]"
];
答案 5 :(得分:0)
即使我的视图看起来不错,但潜在的CGRect错误,但宽度却不正确。
使用CAShapeLayer
为我的单元格添加边框时,这一点很明显。
我通过在layoutIfNeeded()
上调用contentView
来解决它
override func layoutSubviews() {
super.layoutSubviews()
contentView.layoutIfNeeded()
}