【问题标题】:swift: not identical to ‘UIPickerView’swift:与“UIPickerView”不同
【发布时间】:2014-12-13 06:03:59
【问题描述】:

我尝试了以下在 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;

        }

}

【问题讨论】:

  • 3个参数的pickerView()方法的返回时间是多少
  • 请给你看完整的代码..
  • pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];显示完整代码。使用上面的方法再次获得正确的数据,因为数据可能需要再次计算。
  • 显示创建标题的方法的整个 Swift 实现。
  • pickerView(_:titleForRow:forComponent:) pickerView(_:attributedTitleForRow:forComponent:) pickerView(_:viewForRow:forComponent:reusingView:) 以上三个也可以显示pickerview的内容,但是不能同时使用,我莫名其妙用了两个。现在只使用最后一个,问题就解决了。谢谢大家。顺便说一句,它确实适用于 obj-c,所以我认为 swift 可以。

标签: swift uipickerview


【解决方案1】:

我想你想打印从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的引用。

希望这会对你有所帮助。

【讨论】:

  • 我需要再次使用[self pickerView:pickerView titleForRow:row forComponent:component]获取重新计算的标题数据
  • 我按照你给出的参考,找到了原因。谢谢。
猜你喜欢
  • 1970-01-01
  • 2017-07-16
  • 2019-02-15
  • 1970-01-01
  • 1970-01-01
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多