2015-10-26 375 views
0

我在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?

+1

模型 - 視圖 - 控制器。模型 - 視圖 - 控制器。不要使用單元格(視圖)來保持狀態。當用戶進行更改時,請更改_model_。從_model_中選取控制器中的更改。 – matt

+0

是的,這就是爲什麼我稱之爲「搖動狗」,並完全不滿意! –

回答

1

看着你的解決方案我認爲你會遇到一個問題,因爲單元格持有對viewController的引用,所以會導致內存泄漏。你應該使用「弱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 
} 
} 
+0

謝謝,你的回答真的有幫助! 更多的,我現在看到,我不明白非常重要的「委託」編程的一部分。 –

+0

有一件事:Xcode咆哮在我身上: '弱'不能應用於非類類型'CustomTableViewCellDelegate',所以我在這裏刪除它 /*弱*/var委託:... –

+0

最好使用弱。添加弱回來,但改變協議爲協議CustomTableViewCellDelegate:class { – totiG