私はiOS開発が初めてで、カスタムUITableViewCellを作成したいと考えていました。私は、IBではなくコードでレイアウトを行うことを好む。カスタムセルクラスを作成し、テスト用にUIImageViewのみを追加しました。私は、次の制約を適用:プログラムでUITableViewCellをレイアウトする
-(void)layoutSubviews {
NSDictionary *views = @{
@"view": self.mCoverImage
};
NSArray* horizontalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:NSLayoutFormatAlignmentMask metrics:nil views:views];
NSArray* verticalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view(170)]|" options:NSLayoutFormatAlignmentMask metrics:nil views:views];
[self.contentView addConstraints:horizontalConstraint];
[self.contentView addConstraints:verticalConstraint];
}
をが、これは、コンソールで次のように私を与えた:
2016-03-30 11:37:08.818 Testing3[87057:1179225] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x7fb033e721d0 h=--& v=--& V:[naruto(0)] (Names: naruto:0x7fb033fb4c70)>",
"<NSLayoutConstraint:0x7fb033e6fab0 V:[naruto(170)] (Names: naruto:0x7fb033fb4c70)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fb033e6fab0 V:[naruto(170)] (Names: naruto:0x7fb033fb4c70)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2016-03-30 11:37:08.819 Testing3[87057:1179225] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
誰が私のコードで間違って何を教えてもらえますか?
EDIT:
#import "TopPointsController.h"
#import "TestCell.h"
@interface TopPointsController()
@property (strong, nonatomic) IBOutlet UITableView *mainTable;
@end
@implementation TopPointsController
- (void)viewDidLoad {
[super viewDidLoad];
self.mainTable.rowHeight = UITableViewAutomaticDimension;
self.mainTable.estimatedRowHeight = 50.0;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 50;
}
-(UITableViewCell*)tableView:(UITableView*) tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
TestCell* cell = [tableView dequeueReusableCellWithIdentifier:@"TestCell"];
if(cell == nil) {
cell = [[TestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TestCell"];
}
return cell;
}
@end
:
これは、テーブルビューdelagateです:
はちょうどそれが簡単にdelagateのための完全なソースコードならびに細胞implemenationの下に見つけてくださいようにします
これはセルの実装です:
#import "TestCell.h"
@implementation TestCell
- (void)awakeFromNib {
// Initialization code
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.mCoverImage = [[UIImageView alloc] init];
UIImage* image = [UIImage imageNamed:@"naruto"];
self.mCoverImage.image = image;
[self.contentView addSubview:self.mCoverImage];
NSDictionary *views = @{
@"view": self.mCoverImage
};
self.mCoverImage.translatesAutoresizingMaskIntoConstraints = NO;
NSArray* horizontalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:NSLayoutFormatAlignmentMask metrics:nil views:views];
NSArray* verticalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view(170)]" options:NSLayoutFormatAlignmentMask metrics:nil views:views];
[self.contentView addConstraints:horizontalConstraint];
[self.contentView addConstraints:verticalConstraint];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
@end
あなたは 'translatesAutoresizingMaskIntoConstraints'を有効にしていません。制約を追加する前にfalse/NOに設定してください – GoodSp33d