swift:与 ‘UIPickerView’ 不同

标签 swift uipickerview

我尝试了以下在 Obj-C 中有效的方法,但我在 swift 中遇到了错误: '(@lvalue UIPickerView!, titleForRow: Int, forComponent: Int) -> $T7' 与 '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;

        }

}

最佳答案

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

这是给你的几个例子:

    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

}

引用自HERE .

希望对您有所帮助。

关于swift:与 ‘UIPickerView’ 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27456061/

相关文章:

ios - 如何在 uitextfield 中隐藏 UIPickerView?

swift - 从 viewcontroller extension 引用 viewcontroller outlets

ios - 将 View 作为 UITableViewCell 放在 tableView 中是一种好方法吗?

ios - 如何保存用户使用 swift 选择的图像?

ios - 选择器 View 函数未调用

ios - UIPicker selectRow 不工作-这段代码有什么问题?

ios - 如何在tableview cell swift 3.0中设置选中的复选标记

ios - 谷歌登录登录 View Controller 而不是 Appdelegate IOS Swift

ios - 带有文本字段的 uipickerview 在 textFieldShouldBeginEditing 之后未被调用

iphone - 有没有像scrollviewDidScroll这样的uipickerview委托(delegate)方法?