2016-03-30 6 views
0

私は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 
+0

あなたは 'translatesAutoresizingMaskIntoConstraints'を有効にしていません。制約を追加する前にfalse/NOに設定してください – GoodSp33d

答えて

1

問題はここにある:

V:|[view(170)]| 

基本的に、あなたは彼らがスーパーに固定されているように上下に垂直方向にストレッチするビューを指示していますが、あなたはまた、高さが170ポイントでなければならないと言っています。
セルの高さが170ポイントよりも大きいか小さい場合は、制約のセットを満たすことができません。
は、あなたができることの問題を解決するには、次の

  • 削除の高さの制約V:|[view]|
  • ので、もし
  • 、これらの制約の優先度の低い方は、二人の間競合がある上部または下部制約V:[view(170)]|V:|[view(170)]を削除しますより優先度の高いものが勝ちますV:[view([email protected])]|

    さらに、追加した画像ビューでtranslatesAutoresizingMaskIntoConstraintsをfalseに設定するように注意してください。


layoutSubviewに何の追加の制約もしないし、常にあなたがそれをオーバーライドしている場合super.layoutSubviews()を呼び出します。
layoutSubviewsは何度も呼び出すことができ、そのたびにビューに制約を追加します。 awakeFromNib,またはupdateConstraintsの制約を追加してください(ただし、一度だけ注意してください)

+0

セルを170に設定する方法は?なぜなら、これは動的なセルの高さを作る方法だということです。 – m2008m1033m

+0

依存して、特定のインデックスパスの高さを求めるデリゲートメソッドがあるか、またはUITableViewにrowHeightプロパティがあります。
自動セルサイジングを使用している場合は、完全なコードを投稿して何か問題があるかどうかを確認してください。 – Andrea

+0

私はデリゲートでそれを行う方法を知っていますが、セル自体のサイズを設定したいと思います。私は、画像ビューがセルのコンテンツビューにピン止めされた上端、下端、後端を持つべきであることをどこかで読んだ。私はIBでこれを試してみましたが、うまくいきましたがコードではできませんでした。 – m2008m1033m

関連する問題