我有3個自定義單元格,它們都是從viewController Uitableview子類化的。單元格中有手勢識別器來查看它們被觸摸的位置以更改圖標/背景,然後將結果保存到核心數據。 我想要保存數據,並且在沒有任何單元格被觸摸的情況下,例如3秒(刪除保存按鈕),單元格重置爲基本顏色和圖形。我已經完成了所有工作,但是我無法在同一時間重新設置所有方法。我嘗試了很多不同的方式,我認爲最好的方式是在視圖控制器中重新加載表格,但是我無法使其工作。 目前我正在使用一個結構與一個布爾感動。當觸摸在任何單元格結束時會調用一個定時器,如果它沒有另一次觸摸就結束,它會調用保存和重置的方法。 也許這是做錯的方法,所以我有點卡住了。Swift:同時重置多個子類自定義uitableviewcells?
視圖控制器類
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->
Int {
return measurements.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("BmCustomCell", forIndexPath: indexPath) as! BmTableViewCell
return cell
} else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCellWithIdentifier("StressCustomCell", forIndexPath: indexPath)as! StressTableViewCell
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("PainCustomCell", forIndexPath: indexPath) as! PainTableViewCell
return cell
}
}
painTableViewCell類的樣品(同另外兩個)
// if touched the text changes and the icon changes. if not touched for 5 seconds resets to not touched.
func resetTouchedPainAfterFiveSecs(){
if TouchNotification.isBeingTouched == false {
timer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: Selector("reset"), userInfo: nil, repeats: false)
} else {
timer.invalidate()
}
}
func reset(){
ResetTables().resetAllTables()
savePainData()
}
// reset pain ui to ready to go state
func resetPain() {
TouchNotification.reset = true
if TouchNotification.reset == true {
painLabel.textColor = whiteColour
painIcon.image = UIImage(named: "IconPain 0 Unused")
touchedPain = false
painLabel.text = "Pain"
TouchNotification.reset = false
}
}
TouchNotification
struct TouchNotification {
static var isBeingTouched = false
static var reset = false
}
重置類
class ResetTables: UIViewController {
func resetAllTables(){
StressTableViewCell().resetStress()
BmTableViewCell().resetBM()
PainTableViewCell().resetPain()
}
}
單元格根據觸摸它們的位置更改顏色和圖標。越往右邊,結果越高。我會保留在自定義單元類中的計算嗎? – SashaZ
您必須更新模型。您可以爲單元設置一些委託協議以通知視圖控制器,以便它可以相應地更新模型,或者直接將點按手勢/插座掛接到視圖控制器。你可以讓單元格直接更新它的顏色(並放棄我在'didSelectRowAtIndexPath'中做的'updateCellsAtIndexPaths'),但是如果單元格滾動然後重新開啓,觸發'cellForRowAtIndexPath'成爲再次呼叫。 – Rob
謝謝明天再去。 – SashaZ