2009-01-06 109 views
3

我有一個UIPickerView作爲UITableViewController中的子視圖,我想在點擊某個行時將其滑動到位。我已經實現了必要的數據源和委託方法,但pickerview的行爲奇怪。它的行爲就像它被機械卡住,它不能正確旋轉,它只是移動一點。如果我嘗試了幾次旋轉它,我得到的調試器控制檯此消息:UIPickerView添加到uitableviewcontroller.view將不會旋轉

SendDelegateMessage:委託失敗 等待10秒鐘後返回。 主運行循環模式: UITrackingRunLoopMode如果您不是 使用整個觸摸屏 區間(這可以延長這個 等待),請提交一個錯誤。

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

- (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在同一個應用程序的另一個視圖中保持不變。 有什麼建議嗎? 乾杯,

回答

1

你不是真的使用viewDidLoad中的代碼添加選擇器到UITableView。 UITableView只能在UITableViewCells中有控件/行,這些在cellForRowAtIndex方法中指定。我們不能像UIView那樣向UITableView添加控件。

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

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

+0

非常感謝,這個問題讓我瘋狂。事實上,我懷疑UITableViewController可能是罪魁禍首,但並沒有試圖改變它。我現在意識到我只是添加了5行代碼來測試我的疑問。 乾杯,大衛 – nutsmuggler 2009-01-07 17:06:22