2015-02-05 47 views
0

我已经创建了一个带有动态原型单元格的tableviewcontroller。在此视图中,用户可以选择从按下按钮来键入位置评论,也可以使用UI滑块为位置评分为1至10。该审查是在tableviewcontroller内使用UIAlertController完成的 - 但UISlider在单元本身内。我试图将这两个数据片段保存到tableviewcontroller中的核心数据。但是UISlider评分值在tableviewcontroller中不可用 - 是否有一种方法可以在单元格的tableview中引用它,还是需要有两个单独的保存函数?以下是我的一些代码 - 在tableview控制器中它不能识别分配给原型单元格中的UISLider值的变量。预先感谢任何帮助!在UITableViewController中保存CoreData,但UISlider数据仅在原型单元格中 - 我如何将它保存到CoreData中

在我tableviewcell:

class WineryInfoTableViewCell: UITableViewCell { 

@IBOutlet weak var ratingLabel: UILabel! 
@IBOutlet weak var sliderbutton: UISlider! 

@IBAction func sliderValueChanged(sender: UISlider) { 
    var ratingValue = Float(sender.value) 
    var roundedRatingValue = roundf(ratingValue/0.5)*0.5 
    ratingLabel.text = "\(roundedRatingValue)" 

} 

我tableviewcontroller

@IBAction FUNC保存(){

if let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext { 

     myRatingData = NSEntityDescription.insertNewObjectForEntityForName("WineryReview", inManagedObjectContext: managedObjectContext) as wineryReview 

     myRatingData.wineryName = wineryNames 
     //myRatingData.rating = ratingLabel.text how do I call this for saving? 
     myRatingData.review = myRatingEntry 


     var e:NSError? 
     if managedObjectContext.save(&e) != true { 
      println("insert error: \(e!.localizedDescription)") 
      return 
     } 
    } 

    // If all fields are correctly filled in, extract the field value 
    println("Winery: " + myRatingData.wineryName) 
    println("Review: " + myRatingData.review) 
    //println("Rating: " + ratingLabel.text!) how do I call this for saving? 

} 

回答

0

我还是新的雨燕,但我想想我可能会回答你的困境的一部分。要引用表格单元格中的UISlider,您可以使用'标记'来获取对它的引用。

例如:

let cell = tableView.dequeueReusableCellWithIdentifier(TableViewCellIdentifiers.someCell, forIndexPath: indexPath) as UITableViewCell

let slider = cell.viewWithTag(100) as UISlider

在上面的例子中,我会用分配UISlider为100标签,所以我能够用得到一个参考吧cell.viewWithTag(100)。

请原谅我,如果这不起作用!

0

您可以将WineryReview对象传递给单元格,并直接从sliderValueChanged设置其评分。

在cellForRowAtIndex:

cell.wineryReview = myReviewData 

在TableViewCell:

@IBOutlet weak var ratingLabel: UILabel! 
@IBOutlet weak var sliderbutton: UISlider! 

var wineryReview: WineryReview? 

@IBAction func sliderValueChanged(sender: UISlider) { 
    var ratingValue = Float(sender.value) 
    var roundedRatingValue = roundf(ratingValue/0.5)*0.5 

    wineryReview.rating = roundedRatingValue 

    ratingLabel.text = "\(roundedRatingValue)" 
} 

你可以把节省从TableViewController的背景下,如果你喜欢。我在一个项目中做了非常类似的事情。