如何在 TableViewCell 中获取更改后的值

How to reach changed values in TableViewCell

我在 Swift 方面不是很有经验。我有一个 tableView 和自定义单元格,其中有几个标签、UISlider 和 UISwitch。 当我更改滑块值并点击提交(栏按钮项目)时,我想从所有单元格中收集 UISlider 和 UISwitch 值。
我尝试了什么:
1.Tags: 我到达了一些单元格,但是停了下来,无法到达当前不可见的单元格,最后看了一些意见,标签不太可能使用。
问:是否有明确的赞成与反对意见?
2.CellForRowAtIndexPath:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {    
let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell") as! CustomTableViewCell 
cell.Label1.text = "Long Tongue, size \(indexPath.row) cm"
cell.Label2.text = "Big Banana, size \(indexPath.row) inches"
return cell
}

    @IBAction func submitTapped(sender: AnyObject) {       
let cell = tableView(self.tableView , cellForRowAtIndexPath: NSIndexPath(forRow: 1, inSection: 0)) as! CustomTableViewCell
print(cell.Label1.text) //  gives me 
print(cell.Label2.text) //   values

print(cell.customSlider.value)  // gives me the value stated as  
print(cell.customSwitch.on)     // default
}

我理解正确吗,我在这里调用了 cellForRowAtIndexPath,难怪我得到了自定义单元格的新实例(由函数处理)?

3."Wag the dog" 不幸的是,我失去了一个 SO link 来讨论这个解决方案:(
我尝试使用 .superview.superview... 访问 UIViewController,但是 Xcode 拒绝吃掉 4 个超级视图(而且我不确定我是否找到了正确数量的 .superviews)。

主要思想是在自定义单元格中授予对 UIViewController 属性 的访问权限:

在 CustomTableViewCell 中添加一个 属性:

class CustomTableViewCell: UITableViewCell {        
var viewController : MyViewController?
var cellNo = 0
//and so on
}

class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var sliderValues: [Float] = []

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {    
let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell") as! CustomTableViewCell 
cell.Label1.text = "Long Tongue, size \(indexPath.row) cm"
cell.Label2.text = "Big Banana, size \(indexPath.row) inches"
self.sliderValues.append (0.0) // just to be sure that each slider can put it's value to Array

cell.cellNo = indexPath.row // so the Custom Cell knows it's No

//---------------------//
cell.viewController = self
//---------------------//

return cell
    }
}

//----------------------
//and back to Custom Cell
@IBAction func sliderValueChanged(sender: AnyObject) {
    self.viewController?.sliderValues[self.cellNo] = self.customSlider.value
}

// and the same way with UISwitch

好消息,这有效!
问题:是否有任何方法不 "wag the dog" 并从 UIViewController 到达自定义单元格?

查看您的解决方案,我认为您会遇到问题,因为单元格持有对您的 viewController 的引用,因此会导致内存泄漏。如果你要走这条路,你应该使用 "weak var viewController : MyViewController?"

但是,正如马特所说,您不应该这样做。最好用这些数据更新你的模型。您可以将数据直接传递给单元格以修改数据,但我不知道您的数据格式,所以另一个想法是您可以创建一个委托来将值从单元格传回。一个例子是:

protocol CustomTableViewCellDelegate: class {
    func didChangeSlider(value: Float, cellNo: Int)
    //func didSwitchOn(value: Bool, cellNo: Int)
}

然后您可以将其添加到您的单元格中,如下所示:

class CustomTableViewCell: UITableViewCell {        
weak var delegate: CustomTableViewCellDelegate?
var cellNo = 0
//and so on
}

然后在这里使用委托:

@IBAction func sliderValueChanged(sender: AnyObject) {
    self.delegate?.didChangeSlider(self.customSlider.value, cellNo)
}

最后在你的 ViewController 创建单元格时,你需要这样做:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {    
let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell") as! CustomTableViewCell 
cell.Label1.text = "Long Tongue, size \(indexPath.row) cm"
cell.Label2.text = "Big Banana, size \(indexPath.row) inches"
cell.delegate = self
cell.cellNo = indexPath.row
return cell
}

在您的 ViewController 末尾添加,添加委托处理程序:

extension MyViewController: CustomTableViewCellDelegate {
func didChangeSlider(value: Float, cellNo: Int) {
//Save your value here
}
}