I'm trying to cleanup my code. I have 3 pickerView in my ViewController and I want to have implementation of delegates in separate files.
id imp = [[HourPickerImplementation alloc] init];
self.hourPicker.dataSource = imp;
self.hourPicker.delegate = imp;
HourPickerImplementation.h:
@interface HourPickerImplementation : NSObject <UIPickerViewDataSource, UIPickerViewDelegate>
@end
HourPickerImplementation.m:
@implementation HourPickerImplementation {
NSMutableArray *_pickerData;
}
- (id)init {
_pickerData = [[NSMutableArray alloc] initWithCapacity:25];
for (int i = 0; i < 25; i++) {
[_pickerData addObject:[NSString stringWithFormat:@"%d H.", i]];
}
return self;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
return _pickerData.count;
}
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view {
UIImage *img = [UIImage imageNamed:@"icon_lock.png"];
UIImageView *temp = [[UIImageView alloc] initWithImage:img];
temp.frame = CGRectMake(51, 20, 20, 20);
UILabel *channelLabel =
[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 60)];
channelLabel.text =
[NSString stringWithFormat:@"%@", [_pickerData objectAtIndex:row]];
channelLabel.textColor = [UIColor whiteColor];
channelLabel.backgroundColor = [UIColor clearColor];
UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 60)];
[tmpView insertSubview:temp atIndex:0];
[tmpView insertSubview:channelLabel atIndex:1];
return tmpView;
}
Unfortunately it gives me that error:
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x170099c80 V:[UIImageView:0x12751a110(55)]>
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.
2015-08-26 10:30:30.489 bollywood[675:103276] 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)
(
"<NSLayoutConstraint:0x17009a540 V:[UIImageView:0x12751c480(55)]>",
"<NSLayoutConstraint:0x17009a590 V:[UIImageView:0x12751c5c0(50)]>",
"<NSLayoutConstraint:0x17009a810 V:[UIImageView:0x12751c480]-(12)-[UIImageView:0x12751c5c0]>",
"<NSLayoutConstraint:0x17009a900 UITableViewCellContentView:0x12751bcd0.bottomMargin == UIImageView:0x12751c5c0.bottom>",
"<NSLayoutConstraint:0x17009aa40 UIImageView:0x12751c480.top == UITableViewCellContentView:0x12751bcd0.topMargin + 12>",
"<NSLayoutConstraint:0x1740961c0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x12751bcd0(139)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x17009a540 V:[UIImageView:0x12751c480(55)]>
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.
What am I doing wrong? It works in one file but it looks awful.
The error is implying that the constraints in your cell can not be satisfied because groups of them as asking for layout that can not be achieved.
Looking at the output, the following line is probably the issue:
<NSLayoutConstraint:0x1740961c0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x12751bcd0(139)]>
This constraint is likely generated by the picker view based on what you are returning from rowForHeightForComponent
. This height seems to be 139pts.
First check that your delegate method is actually being called. You said you split the files. A guess would be that the system is setting it to 139 because it is not seeing your new delegate code since it has moved.
Your other constraints seem to be 2 images of height 55 and 50, a spacer of 12 between and a top margin of 12. Total fixed height in constraints is 117. I notice you are also using layout to margin. If that is not what you intended, change that to be against the actual top.
So your issue is the cell contents want to be packed at fixed sizes/spacings but cannot still be aligned with the top and bottom when its set to 139pts.
My suggestion would be to check your calculation for row height that you return.
You can fix the constraint issue by changing the bottom constraint you have for the bottom image and bottom of the content view being aligned, to one where the bottom of the image is being pinned >=0 from the bottom of the content view. By making this pinned >=0 the constraints will no longer fail. What you will see though is a gap at the bottom which you now allow to cope with the mismatch between the height you return in code.