该申请是一项满意度调查,所有领域必须由人填补。我使用了一个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()
答案 0 :(得分:1)
反过来尝试,先滚动然后获取单元格:
let indexPath = NSIndexPath(forRow: 1, inSection: 0)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: false)
let cellPb = self.tableView.cellForRowAtIndexPath(indexPath)
答案 1 :(得分:0)
cellPb!.textLabel!.textColor = UIColor.redColor()
以上线强行打开该值。这就是崩溃的原因。所以用 如果让这是打开任何值的安全方式
试试此代码
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)
}