swift:与“UIPickerView"不同 [英] swift: not identical to ‘UIPickerView’

查看:25
本文介绍了swift:与“UIPickerView"不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下适用于 Obj-C 的方法,但在 swift 中出现错误:'(@lvalue UIPickerView!, titleForRow: Int, forComponent: Int) -> $T7' 与'UIPickerView' 不同

I tried the following method which works in Obj-C, but I got error in swift: '(@lvalue UIPickerView!, titleForRow: Int, forComponent: Int) -> $T7' is not identical to ‘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 的工作代码:

here is the obj-c working codes:

- (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;

        }

}

推荐答案

我认为您想打印从 UIPickerView 中选择索引的数据,但我认为您这样做是错误的.

I think you want to print the data from which index is selected from UIPickerView,But I think you are doing this wrong way.

这里有两个例子:

    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
       }
}

参考来自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

}

参考来自这里.

希望对您有所帮助.

这篇关于swift:与“UIPickerView"不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆