【问题标题】:Swift: Reseting multiple subclassed custom uitableviewcells at the same time?Swift:同时重置多个子类自定义 uitableviewcells?
【发布时间】: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


    【解决方案1】:

    看起来问题在于您正在创建对象的新实例,而不是使用屏幕上的对象。

    举个例子,

    ResetTables().resetAllTables()

    哪个调用

    func resetAllTables(){
      StressTableViewCell().resetStress()
      BmTableViewCell().resetBM()
      PainTableViewCell().resetPain()
    }
    

    您对ResetTables() 所做的是创建一个ResetTables 类的新对象,然后在其上调用resetAllTables(),这又会创建一个新单元格StressTableViewCell,并调用resetStress在上面。其他两个单元格也是如此。虽然您正在调用这些方法,但您并不是在显示的单元格上调用它们,而是在函数离开作用域后被释放的新单元格上调用它们。

    因此,在您的 reset() 方法中,您希望重置单元格本身,而不是创建您重置的新单元格。

    【讨论】:

    • 是的,这是我从收到的错误消息中想到的。
    【解决方案2】:

    您说“我认为最好的方法是在视图控制器中重新加载表格,但我无法让它工作”。是的,这正是我建议你做的事情(尽管我可能会建议 reloadRowsAtIndexPaths 代替)。

    一个更微妙的问题是,尤其是当您的 UI 变得越来越复杂时,单元格可以滚动到屏幕之外,可以重复使用等。因此,您通常希望避免直接更新单元格。如果这些单元格中的一个或多个在需要重置时已经滚出屏幕怎么办?当它滚动回视图时,您如何知道如何处理该单元格?等等。

    因此,您经常希望将 UI(单元格)与模型(有关哪些单元格是什么颜色的信息)解耦。一旦你这样做了,你就可以移动到一个你有一些模型结构的世界(在我下面的例子中,cellBackgroundColors),它跟踪一个单元格应该是什么颜色。然后cellForRowAtIndexPath 将根据这个模型对象设置单元格的颜色。然后,每当您想更改单元格的颜色时,您 (a) 更新此模型对象,然后 (b) 告诉表格视图重新加载该单元格。

    如果您想同时更新多个单元格,则更新您的模型并使用一组单元格调用reloadRowsAtIndexPaths,然后它们将同时更新。但是您也将以安全的方式执行此操作,这不依赖于当时屏幕上的单元格。而且,如果我正确推断您希望计时器在他们点击其他内容时重新启动,您可以invalidate 计时器并在用户每次点击单元格时创建一个新计时器。

    这是一个小例子:

    class ViewController: UITableViewController {
    
        var cellBackgroundColors: [UIColor]!
        let defaultColor = UIColor.whiteColor()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            resetCellBackgroundColorsToDefault()
        }
    
        func resetCellBackgroundColorsToDefault() {
            cellBackgroundColors = [UIColor.redColor(), UIColor.yellowColor(), UIColor.cyanColor()]
        }
    
        // MARK: UITableViewDataSource
    
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 100
        }
    
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    
            // do whatever cell configuration you want here
    
            // but programmatically set the background color (or whatever) based upon the model:
    
            if indexPath.row < 3 {
                cell.backgroundColor = cellBackgroundColors[indexPath.row]
            } else {
                cell.backgroundColor = defaultColor
            }
    
            return cell
        }
    
        // MARK: UITableViewDelegate
    
        override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
            tableView.deselectRowAtIndexPath(indexPath, animated: false)
    
            cellBackgroundColors[indexPath.row] = UIColor.blueColor()
    
            tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    
            timer?.invalidate()
            timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "resetCells:", userInfo: nil, repeats: false)
        }
    
        // MARK: Timer code
    
        weak var timer: NSTimer?
    
        func resetCells(timer: NSTimer) {
            resetCellBackgroundColorsToDefault()
    
            let indexPaths = [0, 1, 2].map { NSIndexPath(forRow: $0, inSection: 0) }
    
            tableView.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: .Fade)
        }
    
    }
    

    【讨论】:

    • 单元格会根据您触摸它们的位置而改变颜色和图标。越往右走,结果就越高。我会在自定义单元类中保留该计算吗?
    • 您必须更新模型。您可以为单元设置一些委托协议以通知视图控制器,以便它可以相应地更新模型,或者将点击手势/插座直接连接到视图控制器。您可以让单元格直接更新其颜色(并放弃我在didSelectRowAtIndexPath 中所做的updateCellsAtIndexPaths),但您仍然希望更新模型以防单元格滚动然后重新打开,从而触发cellForRowAtIndexPath再次调用。
    • 谢谢,明天试试。
    猜你喜欢
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 2010-10-04
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多