- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个 UITableView
,其中的单元格多于屏幕上显示的单元格。当我收到来 self 的数据模型的通知时,我想跳转到特定行并显示一个非常基本的动画。
我的代码是:
func animateBackgroundColor(indexPath: NSIndexPath) {
dispatch_async(dispatch_get_main_queue()) {
NSLog("table should be at the right position")
if let cell = self.tableView.cellForRowAtIndexPath(indexPath) as? BasicCardCell {
var actColor = cell.backgroundColor
self.manager.vibrate()
UIView.animateWithDuration(0.2, animations: { cell.backgroundColor = UIColor.redColor() }, completion: {
_ in
UIView.animateWithDuration(0.2, animations: { cell.backgroundColor = actColor }, completion: { _ in
self.readNotificationCount--
if self.readNotificationCount >= 0 {
var legicCard = self.legicCards[indexPath.section]
legicCard.wasRead = false
self.reloadTableViewData()
} else {
self.animateBackgroundColor(indexPath)
}
})
})
}
}
}
func cardWasRead(notification: NSNotification) {
readNotificationCount++
NSLog("\(readNotificationCount)")
if let userInfo = notification.userInfo as? [String : AnyObject], let index = userInfo["Index"] as? Int {
dispatch_sync(dispatch_get_main_queue()){
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: index), atScrollPosition: .None, animated: true)
self.tableView.layoutIfNeeded()
NSLog("table should scroll to selected row")
}
self.animateBackgroundColor(NSIndexPath(forRow: 0, inSection: index))
}
}
我希望 dispatch_sync 部分会延迟我的 animateBackgroundColor
方法的执行,直到滚动完成。不幸的是,情况并非如此,因此当行不可见时调用 animateBackgroundColor
-> cellForRowAtIndexPath
返回 nil
并且我的动画不会发生。如果不需要滚动,则动画可以正常工作。
谁能告诉我如何延迟执行我的 animateBackgroundColor
函数直到滚动完成?
非常感谢和亲切的问候
最佳答案
延迟动画似乎不是一个好的解决方案,因为 scrollToRowAtIndexPath
动画持续时间是根据当前列表项到指定项的距离设置的。要解决此问题,您需要在 scrollToRowAtIndexPath
动画完成后执行 animateBackgroudColor,方法是实现 scrollViewDidEndScrollingAnimation
UITableViewDelegate 方法。这里棘手的部分是获取 tableview 滚动的 indexPath。可能的解决方法:
var indexPath:NSIndexpath?
func cardWasRead(notification: NSNotification) {
readNotificationCount++
NSLog("\(readNotificationCount)")
if let userInfo = notification.userInfo as? [String : AnyObject], let index = userInfo["Index"] as? Int{
dispatch_sync(dispatch_get_main_queue()){
self.indexPath = NSIndexPath(forRow: 0, inSection: index)
self.tableView.scrollToRowAtIndexPath(self.indexPath, atScrollPosition: .None, animated: true)
self.tableView.layoutIfNeeded()
NSLog("table should scroll to selected row")
}
}
}
func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) {
self.animateBackgroundColor(self.indexPath)
indexPath = nil
}
关于ios - 等到 scrollToRowAtIndexPath swift 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30641801/
我正在尝试滚动表格以显示最后一个单元格。我正在使用 scrollToRowAtIndexPath 底部方法,但它花费了太多时间。我该如何解决这个问题? 最佳答案 要使用自定义时间制作动画,您可以使用以
我正在尝试以编程方式滚动表格 View ,如下编码。请注意,我增加了内容大小,因此我可以看到最后几行。我可以正确手动滚动以在顶部获取所需的索引路径,但以编程方式它不会发生。 [tableview re
我有一个非常奇怪的案例,我想听听您的意见。在我的应用程序中,我有 scrollToRowAtIndexPath 以便在侧面索引上使用选项卡时,它会跳转到单元格。直到大约 3 个月前,它一直运行良好,相
UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: section (3) beyond bounds
在我的 UITableView 中,我使用一种方法滚动到最后一个单元格: WLNetworkClient.sharedClient().createCommentWIthText(commentTex
我在使用使用 tableview 的应用程序时遇到错误,错误如下所示。 2012-01-19 10:19:51.442 bcode[1176:207] *** Terminating app due
我在使用 UITableView 时遇到问题,它似乎无法在使用 scrollToRowAtIndexPath:atScrollPosition:animated 时正确处理滚动到最后一行 这是我用来引
我对 UITableView 有疑问,尤其是 scrollToRowAtIndexPath .这里我们分别设置了section-header-height和row-height。稍后,当显示键盘时,我
我有一个 UITableView 作为 View 的 subview 。在填充表时的 ViewController 中,我突出显示其中一行并保留该行的 indexPath 记录。我在 cellforR
我有一个带有自定义单元格的表格 View ,其动态单元格高度取决于单元格内容。 我的问题如下,当我在 viewDidLoad 中以编程方式询问滚动到它工作的给定位置时,除了最后一行。有时该行出现但不完
我注意到 scrollToRowAtIndexPath:atScrollPosition:animated: 不会滚动到当前不可见的单元格,所以如果我有 100 个单元格并且我需要到达第 70 个单元
我有一个带有一些自定义单元格的表格 View 。第一个里面是一个按钮。当按下这个按钮时,我想滚动到我的表格 View 中的某个部分。我如何必须将按钮操作与 tableview 链接? 最佳答案 您可以
我是 IOS 7 的新手,我制作了一个测试应用程序,我想在检测到向左滑动时使用 scrollToRowAtIndexPath。检测到滑动,但我正在为“ self ”定义而苦苦挣扎。我收到错误消息 No
我希望我的表格 View 在第一次进入 View 时滚动到底部,但有时不会滚动到底部,有时会使我的表格消失。 (通常发生在第一次或第二次时)。 这是我的滚动代码: -(void)tableScroll
我有一个包含两个 TextField 的子类 UITableViewCell,我想知道如何使用子类中的 scrollToRowAtIndexPath? 我已经设置了 UITextField 委托(de
我正在使用 scrollToRowAtIndexPath 将我的 tableview 集中在先前选择的单元格上,但是它在手机上而不是在模拟器上测试时崩溃了应用程序。 我是这样设置的 - (void)v
我有一个非常简单的问题,但我无法弄清楚问题所在。 我正在尝试在 UITableView 上添加一个导航栏按钮,它将用户滚动到特定的行。我认为使用以下代码会很简单: @IBAction func scr
这是 How to get notified when a tableViewController finishes animating the push onto a nav stack. 的后续行
在一个应用程序中,我正在提取一个 JSON 数组并使用它来填充一个 UITableView: class TableViewController: UITableViewController, NSU
我有一个 UITableView,其中的单元格多于屏幕上显示的单元格。当我收到来 self 的数据模型的通知时,我想跳转到特定行并显示一个非常基本的动画。 我的代码是: func animateBa
我是一名优秀的程序员,十分优秀!