使用 CABasicAnimation 在 tableView 中崩溃,当 UITableViewCell 不可见时文本颜色
Crash in tableView with CABasicAnimation, textColor when the TableViewCell is not visible
该申请是一项满意度调查,所有字段都必须由人填写。我使用了一个 tableView,我希望,当一个字段未填充时,它会自动滚动到该字段,将 tableviewcell 设置为红色,并摇动 tableviewcell。我找到了所有这些,但问题是当单元格不在屏幕上时它似乎崩溃了(可能是因为单元格在屏幕上不可见)。
可能我必须做一些事情来等待单元格首先可见。我该怎么做?
let cellPb = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: false)
//color
cellPb!.textLabel!.textColor = UIColor.redColor()
//shake the cell
let shake = CABasicAnimation(keyPath: "position")
shake.duration = 0.1
shake.repeatCount = 3
shake.autoreverses = true
shake.fromValue = NSValue(CGPoint: CGPointMake(cellPb!.center.x - 5, cellPb!.center.y))
shake.toValue = NSValue(CGPoint: CGPointMake(cellPb!.center.x + 5, cellPb!.center.y))
cellPb!.layer.addAnimation(shake, forKey: "position")
UIAlertView(title: "Please, fill the blank field.", message: nil, delegate: nil, cancelButtonTitle: "Ok").show()
cellPb!.textLabel!.textColor = UIColor.redColor()
上一行强制解包值。这就是它会崩溃的原因。所以用
if let 这是解包任何值的安全方法
试试这个代码
if let cellPb = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 0)) {
//color
cellPb.textLabel!.textColor = UIColor.redColor()
//shake the cell
let shake = CABasicAnimation(keyPath: "position")
shake.duration = 0.1
shake.repeatCount = 3
shake.autoreverses = true
shake.fromValue = NSValue(CGPoint: CGPointMake(cellPb!.center.x - 5, cellPb!.center.y))
shake.toValue = NSValue(CGPoint: CGPointMake(cellPb!.center.x + 5, cellPb!.center.y))
cellPb.layer.addAnimation(shake, forKey: "position")
UIAlertView(title: "Please, fill the blank field.", message: nil, delegate: nil, cancelButtonTitle: "Ok").show()
} else {
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 0), atScrollPosition: .Top, animated: false)
}
尝试反过来,先滚动然后获取单元格:
let indexPath = NSIndexPath(forRow: 1, inSection: 0)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: false)
let cellPb = self.tableView.cellForRowAtIndexPath(indexPath)
该申请是一项满意度调查,所有字段都必须由人填写。我使用了一个 tableView,我希望,当一个字段未填充时,它会自动滚动到该字段,将 tableviewcell 设置为红色,并摇动 tableviewcell。我找到了所有这些,但问题是当单元格不在屏幕上时它似乎崩溃了(可能是因为单元格在屏幕上不可见)。
可能我必须做一些事情来等待单元格首先可见。我该怎么做?
let cellPb = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: false)
//color
cellPb!.textLabel!.textColor = UIColor.redColor()
//shake the cell
let shake = CABasicAnimation(keyPath: "position")
shake.duration = 0.1
shake.repeatCount = 3
shake.autoreverses = true
shake.fromValue = NSValue(CGPoint: CGPointMake(cellPb!.center.x - 5, cellPb!.center.y))
shake.toValue = NSValue(CGPoint: CGPointMake(cellPb!.center.x + 5, cellPb!.center.y))
cellPb!.layer.addAnimation(shake, forKey: "position")
UIAlertView(title: "Please, fill the blank field.", message: nil, delegate: nil, cancelButtonTitle: "Ok").show()
cellPb!.textLabel!.textColor = UIColor.redColor()
上一行强制解包值。这就是它会崩溃的原因。所以用 if let 这是解包任何值的安全方法
试试这个代码
if let cellPb = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 0)) {
//color
cellPb.textLabel!.textColor = UIColor.redColor()
//shake the cell
let shake = CABasicAnimation(keyPath: "position")
shake.duration = 0.1
shake.repeatCount = 3
shake.autoreverses = true
shake.fromValue = NSValue(CGPoint: CGPointMake(cellPb!.center.x - 5, cellPb!.center.y))
shake.toValue = NSValue(CGPoint: CGPointMake(cellPb!.center.x + 5, cellPb!.center.y))
cellPb.layer.addAnimation(shake, forKey: "position")
UIAlertView(title: "Please, fill the blank field.", message: nil, delegate: nil, cancelButtonTitle: "Ok").show()
} else {
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 0), atScrollPosition: .Top, animated: false)
}
尝试反过来,先滚动然后获取单元格:
let indexPath = NSIndexPath(forRow: 1, inSection: 0)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: false)
let cellPb = self.tableView.cellForRowAtIndexPath(indexPath)