我有一个我要封装的自定义UIPicker,所以我可以重用它。 我想要暴露的唯一事情就是委托协议,该协议将使用Subclassed UIPicker告诉ViewController更改了规模。
这是它在底部应该是什么样子以及它在顶部的样子。
这是我在图片中移动顶部选择器时记录的崩溃信息:
*** Assertion failure in -[UITableViewRowData rectForRow:inSection:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableViewRowData.m:1630
这是封装类的代码。我还没有想出我要编写的委托协议,但我想处理封装类中的UIPickerView dataSource和委托协议。这样我需要做的就是重用它,实现自定义委托协议,它告诉我规模已经改变了。
此代码与viewController一起用作UIPickerView的dataSource和委托。我正在努力学习如何制作可重复使用的对象。
#import "SWScalePickerView.h"
@interface SWScalePickerView()<UIPickerViewDataSource,UIPickerViewDelegate>
@property(nonatomic,retain)NSArray *fractions;
@property(nonatomic,retain)NSDecimalNumber *integer;
@property(nonatomic,retain)NSDecimalNumber *fraction;
@property(nonatomic,retain)NSDecimalNumber *scale;
@end
@implementation SWScalePickerView
@synthesize fractions = fractions_;
@synthesize fraction = fraction_;
@synthesize integer = integer_;
@synthesize scale = scale_;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setDataSource:self];
[self setDelegate:self];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#pragma mark - UIPicker
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
switch (component) {
case 0:
return 10;
break;
case 1:
return [fractions_ count];
break;
default:
break;
}
return 2;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (component) {
case 0:
return [NSString stringWithFormat:@"%d",row];
break;
case 1:
{
//return [NSString stringWithFormat:@"%d",row];
return [fractions_ objectAtIndex:row];
}
break;
default:
break;
}
return @"Fix Something";
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return 55.0;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch (component) {
case 0:
integer_ = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%d",row]];
break;
case 1:
{
NSString *string = [fractions_ objectAtIndex:row];
if ([string isEqualToString:@"--"]) {
fraction_ = [NSDecimalNumber zero];
}
if ([string isEqualToString:@"1/8"]) {
fraction_ = [NSDecimalNumber decimalNumberWithString:@"0.125"];
}
if ([string isEqualToString:@"1/4"]) {
fraction_ = [NSDecimalNumber decimalNumberWithString:@"0.25"];
}
if ([string isEqualToString:@"1/3"]) {
fraction_ = [NSDecimalNumber decimalNumberWithString:@"0.33"];
}
if ([string isEqualToString:@"3/8"]) {
fraction_ = [NSDecimalNumber decimalNumberWithString:@"0.375"];
}
if ([string isEqualToString:@"1/2"]) {
fraction_ = [NSDecimalNumber decimalNumberWithString:@"0.5"];
}
if ([string isEqualToString:@"5/8"]) {
fraction_ = [NSDecimalNumber decimalNumberWithString:@"0.625"];
}
if ([string isEqualToString:@"2/3"]) {
fraction_ = [NSDecimalNumber decimalNumberWithString:@"0.666"];
}
if ([string isEqualToString:@"7/8"]) {
fraction_ = [NSDecimalNumber decimalNumberWithString:@"0.875"];
}
}
break;
}
scale_ = [NSDecimalNumber zero];
scale_ = [integer_ decimalNumberByAdding:fraction_];
NSLog(@"%@",[scale_ stringValue]);
//[scaleButton_ setTitle:[scale_ stringValue] forState:UIControlStateNormal];
return;
}
答案 0 :(得分:0)
这是解决方案。因为我正在使用接口构建器,所以我必须在initWithCoder方法中初始化dataSource和委托,而不是initWithFrame。这解决了第一个错误。
要使此自定义选择器将比例返回到正在使用它的viewController,您可以使viewController符合协议。然后在自定义类中的didSelectRow委托方法中,调用视图控制器要实现的方法。它现在被称为“dancePuppets”。那是Bel Air的新鲜王子笑话。这就是你可以返回比例的地方。
#import <UIKit/UIKit.h>
@protocol SWScalePickerDelegate <NSObject>
-(void)dancePuppets; //Method that
@end
@interface SWScalePickerView : UIPickerView
{
IBOutlet id<SWScalePickerDelegate> client;
}
@end
#import "SWScalePickerView.h"
@interface SWScalePickerView()<UIPickerViewDataSource,UIPickerViewDelegate>
@property(nonatomic,retain)NSArray *fractions;
@property(nonatomic,retain)NSDecimalNumber *integer;
@property(nonatomic,retain)NSDecimalNumber *fraction;
@property(nonatomic,retain)NSDecimalNumber *scale;
@end
@implementation SWScalePickerView
@synthesize fractions = fractions_;
@synthesize fraction = fraction_;
@synthesize integer = integer_;
@synthesize scale = scale_;
- (id)initWithCoder:(NSCoder *)decoder {
if ((self = [super initWithCoder:decoder])) {
[self setDataSource:self];
[self setDelegate:self];
fractions_ = [[NSArray alloc]initWithObjects:@"--",@"1/8",@"1/4",@"1/3",@"3/8",@"1/2",@"5/8",@"2/3",@"7/8",nil];
integer_ = [NSDecimalNumber zero];
fraction_ = [NSDecimalNumber zero];
//[picker_ selectedRowInComponent:1]
[self selectRow:1 inComponent:0 animated:YES];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setDataSource:self];
[self setDelegate:self];
fractions_ = [[NSArray alloc]initWithObjects:@"--",@"1/8",@"1/4",@"1/3",@"3/8",@"1/2",@"5/8",@"2/3",@"7/8",nil];
integer_ = [NSDecimalNumber zero];
fraction_ = [NSDecimalNumber zero];
//[picker_ selectedRowInComponent:1]
[self selectRow:1 inComponent:0 animated:YES];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#pragma mark - UIPicker
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
switch (component) {
case 0:
return 10;
break;
case 1:
return [fractions_ count];
break;
default:
break;
}
return 2;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (component) {
case 0:
return [NSString stringWithFormat:@"%d",row];
break;
case 1:
{
//return [NSString stringWithFormat:@"%d",row];
return [fractions_ objectAtIndex:row];
}
break;
default:
break;
}
return @"Fix Something";
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return 55.0;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch (component)
{
case 0:
integer_ = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%d",row]];
break;
case 1:
{
NSString *string = [fractions_ objectAtIndex:row];
if ([string isEqualToString:@"--"]) {
fraction_ = [NSDecimalNumber zero];
}
if ([string isEqualToString:@"1/8"]) {
fraction_ = [NSDecimalNumber decimalNumberWithString:@"0.125"];
}
if ([string isEqualToString:@"1/4"]) {
fraction_ = [NSDecimalNumber decimalNumberWithString:@"0.25"];
}
if ([string isEqualToString:@"1/3"]) {
fraction_ = [NSDecimalNumber decimalNumberWithString:@"0.33"];
}
if ([string isEqualToString:@"3/8"]) {
fraction_ = [NSDecimalNumber decimalNumberWithString:@"0.375"];
}
if ([string isEqualToString:@"1/2"]) {
fraction_ = [NSDecimalNumber decimalNumberWithString:@"0.5"];
}
if ([string isEqualToString:@"5/8"]) {
fraction_ = [NSDecimalNumber decimalNumberWithString:@"0.625"];
}
if ([string isEqualToString:@"2/3"]) {
fraction_ = [NSDecimalNumber decimalNumberWithString:@"0.666"];
}
if ([string isEqualToString:@"7/8"]) {
fraction_ = [NSDecimalNumber decimalNumberWithString:@"0.875"];
}
}
break;
}
scale_ = [NSDecimalNumber zero];
scale_ = [integer_ decimalNumberByAdding:fraction_];
NSLog(@"Set Scale to: %@",[scale_ stringValue]);
[client dancePuppets];
//[scaleButton_ setTitle:[scale_ stringValue] forState:UIControlStateNormal];
return;
}
@end