簡體   English   中英

添加到uitableviewcontroller.view的UIPickerView將不會旋轉

[英]UIPickerView added to uitableviewcontroller.view won't rotate

我在UITableViewController中有一個UIPickerView作為子視圖,我想在單擊某一行時向上滑動。 我已經實現了必要的dataSource和委托方法,但是pickerview很奇怪。 它的作用似乎是機械卡住,它不能正常旋轉,它只是移動一點。 如果我嘗試旋轉幾次,我會在調試器控制台中收到此消息:

SendDelegateMessage:委托在等待10秒后無法返回。 主運行循環模式:UITrackingRunLoopMode如果您沒有在整個時間間隔內使用觸摸屏(這可以延長此等待時間),請提交錯誤。

我用Google搜索,無濟於事。 無論如何,這是相關的代碼。 你可能猜到,這是一個數字選擇器(選擇一個百分比值)。

- (void)viewDidLoad {
     [super viewDidLoad];
     percentPicker  = [[UIPickerViewalloc] initWithFrame:CGRectMake(0,420, 320, 200)];
     percentPicker.delegate = self;
     percentPicker.dataSource = self;
     percentPicker.showsSelectionIndicator = YES;
     [self.view addSubview:percentPicker];
 }

- (void)viewWillAppear:(BOOL)animated {
     // sets the rows to the appropriate value
     // etc
}

- (void)startEditingPercentage {
     [UIView beginAnimations :nilcontext:NULL];
     [UIView setAnimationBeginsFromCurrentState:YES];
     [UIViewsetAnimationDuration:kPickerAnimationDuration ];
     percentPicker.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, -220.0);
     [UIViewcommitAnimations];
 }

- (void)stopEditingPercentage {
    NSLog(@"stopEditingPercentage");
    [UIView beginAnimations :nilcontext:NULL];
    [UIViewsetAnimationBeginsFromCurrentState:YES];
    [UIViewsetAnimationDuration:kPickerAnimationDuration];
    percentPicker.transform = CGAffineTransformIdentity;
    [UIView commitAnimations];
}

#pragma mark UIPickerView delegate and dataSource methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return4;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return10;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { 
    return44;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *retval = (UILabel *)view;
    if (!retval) {
        retval= [[UILabel newLabelWithPrimaryColor :[UIColorblackColor ] selectedColor:[ UIColor blackColor] fontSize:22bold:YEStransparent:YES] autorelease];
    }
    retval.frame = CGRectMake(0, 0, 44, 44);
    retval.textAlignment = UITextAlignmentCenter;
    retval.backgroundColor = [UIColor clearColor];
    retval.text = [NSStringstringWithFormat:@"%i", row];
    if (component > 1 ) {
        // rows 2 and 3 are decimal, white on black
        retval.backgroundColor = [UIColor blackColor];
        retval.textColor = [UIColor whiteColor];
    }
    return retval;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSString *selectedValue;
    selectedValue= [NSString stringWithFormat:@"%i%i.%i%i" ,[percentPickerselectedRowInComponent :0], [ percentPickerselectedRowInComponent: 1], [percentPickerselectedRowInComponent:2], [percentPickerselectedRowInComponent:3]];
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    NSString *trimmedValue = [[formatter numberFromString:selectedValue] stringValue];
    percentValue.text = trimmedValue;
    [formatter release];
}

如您所見,我使用transform屬性將拾取器移入和移出(向下)我的主視圖; startEditing和stopEditing方法由teble視圖中的選擇觸發。 然而,出於調試目的,我消除了這些轉換,並將選擇器留在了桌面上,但沒有任何改變。 我還評論了didSelect方法,但這也沒有改變任何東西。 順便說一句,相同的選擇器視圖構造代碼vorkw在同一個應用程序的另一個視圖中。 有什么建議嗎? 干杯,

您並沒有使用viewDidLoad中的代碼將選擇器添加到UITableView。 UITableView只能在UITableViewCells中擁有控件/行,並且這些控件/行在cellForRowAtIndex方法中指定。 我們不能像UIView一樣向UITableView添加控件。

嘗試使用UIView的子類,並將UITableView和picker添加到此UIView子類。

如果您只想使用UITableViewController子類,請創建一個自定義單元格並將選擇器添加到此自定義單元格。 但是如果你靜態添加它,你就無法隱藏/取消隱藏這個單元格。 然后你將不得不使用reloadData。 我建議使用UIView子類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM