Mat*_*ros 3 iphone objective-c uitableview ipad ios
我正在绘制一个相当简单的自定义UITableViewCell - 左侧的缩略图,右侧的名称和链接标签.我希望我的名字和链接标签是单行的,如果文本超出单元格的宽度,则在尾部截断.我很确定我正确地添加了约束,但我的名字和链接标签不会尊重它们.
这就是运行iOS 8的iPhone 4S上的单元格外观:

在我的代码中,我将名称和链接标签的10点尾随约束添加到superview(单元格contentView),然而,请注意,即使在横向,iOS似乎也不遵守约束.
有人能告诉我我做错了什么吗?这是我整个单元格的代码.我以编程方式创建视图,并且在表视图单元格的初始化程序中具有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
Run Code Online (Sandbox Code Playgroud)
这个问题隐藏在
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
Run Code Online (Sandbox Code Playgroud)
删除此代码可以解决您的问题.
如果您不希望将自动调整掩码变为约束 - 请为cell.contentView设置您需要的约束.
| 归档时间: |
|
| 查看次数: |
3288 次 |
| 最近记录: |