【问题标题】:How to reach changed values in TableViewCell如何在 TableViewCell 中达到更改的值
【发布时间】:2015-10-26 21:23:15
【问题描述】:

我在 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.“摇狗” 不幸的是,我失去了讨论此解决方案的 SO 链接:(
我尝试使用 .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

好消息,这行得通!
问题:有什么方法可以不“摇狗”并从 UIViewController 到达 Custom Cell?

【问题讨论】:

  • 模型 - 视图 - 控制器。模型-视图-控制器。不要使用单元格(视图)来保持状态。当用户进行更改时,更改 model。从 model 获取控制器中的更改。
  • 是的,这就是为什么我称它为“摇狗”,并且完全不满意!

标签: ios swift uitableview uiviewcontroller tableviewcell


【解决方案1】:

查看您的解决方案,我认为您会遇到问题,因为单元格持有对您的 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
}
}

【讨论】:

  • 谢谢,您的回答真的很有帮助!而且,我现在明白了,我不了解编程中非常重要的“委托”部分。
  • 一件事:Xcode 对我咆哮:'weak' 不能应用于非类类型'CustomTableViewCellDelegate',所以我在这里删除它 /*weak*/ var delegate: ...
  • 最好用弱。重新添加弱点,但将协议更改为协议 CustomTableViewCellDelegate: class {
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多