2016-06-28 49 views
0

我想顯示一個值,這是我的表視圖控制器中的一個雙。我通過文本字段獲取用戶的值。一個輸入是字符串,另一個是double。我將它存儲在覈心數據中。我使用NSfetchRequest將其顯示到表視圖。表格視圖單元格有兩個標籤,字符串和雙精度值都放在該標籤中。當我只運行字符串值在表視圖中看到不是雙。如何解決這個從UItextfield值加倍值並保存到核心數據

> Blockquote 
     override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let Cell : TableViewCellFixed = tableView.dequeueReusableCellWithIdentifier("cell") as! TableViewCellFixed 

     let data : NSManagedObject = List[indexPath.row] as! NSManagedObject 
     //cellName and cellPrice are UILabels 

     Cell.cellName.text = data.valueForKey("name") as? String 

     Cell.cellPrice.text = data.valueForKey("price") as? String 



     return Cell 
    } 

PS:我想Cell.cellPrice.text = data.valueforkey( 「價格」)爲Double,它提供了一個錯誤:無法分配雙爲String

Blockquote // add to core data

@IBAction func AddSave(sender: UIButton) { 
    let appDel :AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
    let theContext : NSManagedObjectContext = appDel.managedObjectContext 
    let theEnt = NSEntityDescription.entityForName("Entity", inManagedObjectContext: theContext) 

    let newItem = Model(entity : theEnt!,insertIntoManagedObjectContext: theContext) 

    newItem.name = addName.text! 
    let num :Double = (addPrice.text! as NSString).doubleValue 
    newItem.price = num 
    do 
    { 
     try theContext.save() 
    } 
    catch { 

    } 

    print("name \(newItem.name)") 
    print ("price \(newItem.price)") 
    print ("double \(num)") 


    self.navigationController?.popViewControllerAnimated(true) 
} 

在控制檯輸出 名格萊美 價格6.9466761353417e-310 雙600.0

+0

..如果你想顯示雙精度值,你必須把它作爲字符串。 label.text僅限accpet字符串。 –

回答

0

更改您這樣的代碼

coredata

let num :Double = (addPrice.text! as NSString).doubleValue 
let price = NSNumber(double: num) 
ewItem.price = price 

coredata

if let price = (data.valueForKey("price"))?.doubleValue { 
    cell.cellPrice.text = "\(price)" 
} 
else { 
    cell.cellPrice.text = 0 
} 

希望這將幫助你存儲雙檢索雙重價值。

+0

是的,它確實非常感謝你。雖然它顯示爲零,而不是使用輸入的值。我通過文本框從用戶那裏獲得vlaue。爲了將它保存到double類型的核心數據屬性中,我不得不將它轉換爲double類型。我這樣做newItem.price =(addPrice.text!作爲NSString).doubleValue 爲什麼它變成零?是因爲鑄造嗎?感謝您的回答 – yukti

+0

我編輯了我的答案檢查 –

+0

它說錯誤:缺少語言環境。我在data.valueforkey之前添加了如下所示的語言環境:Cell.cellPrice.text = NSString(format:「%f」,locale:data.valueForKey(「price」)as!字符串,並且仍然顯示錯誤,說明不能隱藏anyObeject到NS語言環境類型 – yukti

0

Cell.cellPrice.text = 「\(date.valueForKey(」 價格 「))」

+0

它解決了錯誤,但在顯示時顯示(「%ld」,無)在我的表格視圖中。謝謝你的回答 – yukti

0

如果你想要做一些數學你可以設置一個雙變量對於試試這個

> Blockquote 
      override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let Cell : TableViewCellFixed = tableView.dequeueReusableCellWithIdentifier("cell") as! TableViewCellFixed 

     let data : NSManagedObject = List[indexPath.row] as! NSManagedObject 
     //cellName and cellPrice are UILabels 

     Cell.cellName.text = data.valueForKey("name") as? String 
     let price = data.valueForKey("price") 
     Cell.cellPrice.text = "\(price)" 



     return Cell 
    } 
+0

沒有。仍然空白顯示。謝謝你的回答 – yukti

+0

嘗試新的代碼,它應該適合你。 –

+0

它顯示nil。 – yukti