我在Swift中不是很有經驗。我有一個tableView和自定義單元格,其中有幾個標籤,UISlider和UISwitch。 當我更改滑塊值並點擊提交(bar按鈕項)時,我想從所有單元收集UISlider和UISwitch值。
我試過的東西:
1.標籤:我到了一些細胞,但停下來,無法到達目前不可見的細胞,最後讀了一些意見,這些標籤不太可能用到。
問:是否有任何明確的專業對照?
2.CellForRowAtIndexPath:如何到達TableViewCell中的更改值
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(我不知道,我發現正確的號碼.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_中選取控制器中的更改。 – matt
是的,這就是爲什麼我稱之爲「搖動狗」,並完全不滿意! –