2014-12-13 52 views
1

我試過以下方法在Obj-C中工作,但是我在swift中出錯: '(@lvalue UIPickerView !, titleForRow:Int,forComponent:Int) - > $ T7' 不等同於‘UIPickerView’swift:不同於'UIPickerView'

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView 
    { 
     var pickerLabel = UILabel() 
     pickerLabel.textColor = UIColor.whiteColor() 
     pickerLabel.text = pickerView(pickerView, titleForRow: row, forComponent: component) 
     return pickerLabel 
    } 

這裏的OBJ-C的工作代碼:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ 

    UILabel* pickerLabel = (UILabel*)view; 

    if (!pickerLabel){ 

     pickerLabel = [[UILabel alloc] init]; 

     [pickerLabel setFont:[UIFont fontWithName:@"Helvetica Neue Bold Italic" size:25]]; 

    } 

    pickerLabel.textAlignment = NSTextAlignmentCenter; 

    pickerLabel.textColor = [UIColor whiteColor]; 

    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component]; 

    return pickerLabel; 

} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 

    if (component == 0){ 

       if ([unitType isEqualToString:@"MS"]) { 

      return [thickness objectAtIndex:row]; 

     }else{ 

      NSString *th = [thickness objectAtIndex:row]; 

      float tt = [th floatValue]*0.039370078740157; 

      NSString *new_th = [NSString stringWithFormat:@"%2.2f",tt]; 

      return new_th; 

     } 



    } 

    if(component ==1){ 

     if ([unitType isEqualToString:@"MS"]) { 

      return [weight_data objectAtIndex:row]; 

     }else{ 

      NSString *wei = [weight_data objectAtIndex:row]; 

      float ww = [wei floatValue]*2.205; 

      NSString *new_wei = [NSString stringWithFormat:@"%3.1f",ww]; 

      return new_wei; 

     } 

} 
+0

什麼是pickerView()方法有3個參數的返回時間 – Jageen 2014-12-13 06:16:18

+1

請告訴你完整的代碼.. – 2014-12-13 07:00:28

+0

pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];顯示完整的代碼 。使用上述方法重新獲得正確的數據,因爲數據可能需要重新計算。 – jdleung 2014-12-13 11:47:29

回答

1

我想你想打印從指數從UIPickerView選擇的數據,但我認爲你這樣做是錯誤的。

這裏是幾個例子供大家:HERE

var states : [String]! 
    func pickerView(pickerView: UIPickerView, viewForRow row: Int, 
    forComponent component: Int, reusingView view: UIView!) -> UIView { 
     var lab : UILabel 
     if let label = view as? UILabel { 
      lab = label 
      println("reusing label") 
     } else { 
      lab = MyLabel() 
      println("making new label") 
     } 
     // You can set text this way where states is an array of string. 
     lab.text = self.states[row] 
     lab.backgroundColor = UIColor.clearColor() 
     lab.sizeToFit() 
     return lab 
     } 
} 

refernce另一個例子是:從HERE

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView { 
    var pickerLabel = view as UILabel! 
    if view == nil { //if no label there yet 
     pickerLabel = UILabel() 
     //color the label's background 
     let hue = CGFloat(row)/CGFloat(pickerData.count) 
     pickerLabel.backgroundColor = UIColor(hue: hue, saturation: 1.0, brightness: 1.0, alpha: 1.0) 
    } 
    let titleData = pickerData[row] 
    let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 26.0)!,NSForegroundColorAttributeName:UIColor.blackColor()]) 

    //This way you can set text for your label. 
    pickerLabel!.attributedText = myTitle 

    return pickerLabel 

} 

參考。

希望這會幫助你。

+0

我需要使用[self pickerView:pickerView titleForRow:row forComponent:component]再次獲取重新計算的標題數據 – jdleung 2014-12-13 11:52:50

+0

我按照您給出的參考,找到原因。 Thx。 – jdleung 2014-12-13 14:25:04