【发布时间】:2016-01-06 22:58:30
【问题描述】:
我有 3 个自定义单元格,它们是它们所在的 viewController Uitableview 的子类。这些单元格具有手势识别器,可以查看它们被触摸的位置以更改图标/背景,然后将结果保存到核心数据。 我希望有这样的情况,即当任何单元格没有被触摸 3 秒(删除保存按钮)时,数据被保存并且单元格重置为基本颜色和图形。我已经完成了一切,但我无法使用任何方法来同时重置它们。我尝试了很多不同的方法,我认为最好的方法是在视图控制器中重新加载表格,但我无法让它工作。 目前我正在使用一个带有 bool 的 Struct。当触摸在任何单元格上结束时,它会调用一个计时器,如果它在没有另一个触摸的情况下结束,它会调用一个方法来保存和重置。 也许这是错误的做法,所以我有点卡住了。
查看控制器类
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
}
}
触摸通知
struct TouchNotification {
static var isBeingTouched = false
static var reset = false
}
重置类
class ResetTables: UIViewController {
func resetAllTables(){
StressTableViewCell().resetStress()
BmTableViewCell().resetBM()
PainTableViewCell().resetPain()
}
}
【问题讨论】:
标签: swift uitableview reset custom-cell