【问题标题】:Scrolling to Bottom of UITableView When Messages Load消息加载时滚动到 UITableView 的底部
【发布时间】:2015-05-15 07:01:00
【问题描述】:

我正试图弄清楚一旦来自数据库的消息加载到 UITableView 的底部,如何滚动到它的底部。我尝试过使用tableView.setContentOffset(CGPointMake(0, CGFloat.max), animated: true),但我的表格从包含所有消息变为完全没有。我也试过了

var iPath = NSIndexPath(forRow: messagesArray.count - 1, inSection: 0)
tableView.scrollToRowAtIndexPath(iPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)

但我得到了EXC_BAD_ACCESS code = 1。任何帮助将不胜感激。

编辑:

在 Parse 从 query.findObjectsInBackgroundWithBlock(...) 中找到数据后,我立即将此代码放入我的 viewDidAppear() 函数中,不确定这是否会有所不同。

查询代码如下:

    query.findObjectsInBackgroundWithBlock { (object, error) -> Void in
                    if (error == nil) {
                        for var i = 0; i < object!.count; i++ {
                            var messageObject = Messages()

                            messageObject.messageID = object![i]["messageID"] as! String

                            println("MESSAGE ID")
                            println(messageObject.messageID)

                            messageObject.messageText = object![i]["message"] as! NSString as String

                            println("MESSAGE TEXT")
                            println(messageObject.messageText)

                            messageObject.recipientID.append(object![i]["recipientID"] as! NSString as String)

                            println("RECIPIENT")
                            println(messageObject.recipientID[0])

                            messageObject.senderID = object![i]["senderID"] as! NSString as String
                            messageObject.timeStamp = object![i]["timeStamp"] as! NSString as String

                            println("TIMESTAMP")
                            println(messageObject.timeStamp)

                            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                                self.messagesArray.append(messageObject)
                            })
                        }


                        self.loadObjects()
                        self.tableView.reloadData()
                        self.tableView.setContentOffset(CGPointMake(0, CGFloat.max), animated: true)
                    }
    }

【问题讨论】:

  • 首先检查表是否在 indexpath 的条件下加载。可能是 indexpath 不存在
  • 你从哪里得到EXC_BAD_ACCESS code = 1你能更新日志吗?
  • @VivekMolkar 我在tableView.scrollToRowAtIndexPath(...) 线上得到它
  • 在第二行放一个断点并打印出iPathtableView是什么——这可能会给你一些线索。
  • 所有与 UI 相关的任务都应该在主线程上执行。因此,您所做的是在更新数据源之前设置表视图。我们从错误中吸取教训:-)

标签: ios uitableview swift parse-platform


【解决方案1】:

试试这个

- (void) viewDidAppear:(BOOL)animated
{
  [super viewDidAppear:animated];
  if (tableView.contentSize.height > tableView.frame.size.height) 
    {
      CGPoint offset = CGPointMake(0, tableView.contentSize.height -tableView.frame.size.height);
      [tableView setContentOffset:offset animated:YES];
   }
}

迅速

if tableView.contentSize.height > tableView.frame.size.height
    {
        let offset = CGPoint(x: 0, y: tableView.contentSize.height - tableView.frame.size.height)
        tableView.setContentOffset(offset, animated: true)
    }

【讨论】:

  • 他需要 Swift 的答案。
  • 表格视图中也提供了内置功能,用于底部滚动。那为什么要使用这种逻辑呢?
  • 我试过了,但没有运气,我把这段代码放在我打电话给tableView.reloadData()
【解决方案2】:

请试试这个

在 Objective-c 中

 NSInteger bottomRow = [self.dataArr count] - 1; // this is your count's array.
    if (bottomRow >= 0) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:bottomRow inSection:0];
        [self.tblview scrollToRowAtIndexPath:indexPath
                               atScrollPosition:UITableViewScrollPositionTop animated:animated];
    }

迅速

var bottomRow : NSInteger = dataArr.count-1;
if (bottomRow >= 0) {

            var indexPath : NSIndexPath = NSIndexPath(forRow: bottomRow, inSection: 0)


            tblview.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)

        }

【讨论】:

  • 您是否已将 IBOutlet 与 tableview 连接?
【解决方案3】:

我调用了下面的方法,它对我有用。但是,请确保在重新加载 tableview 之前调用它。

[tableView setContentOffset:CGPointMake(0, CGFLOAT_MAX)]; 
[tableView reloadData];

希望对你有所帮助。

【讨论】:

    【解决方案4】:

    当你的表格被加载后,然后滚动它。

    - (void)viewDidApear{
     //Please convert it into swift.
       [self performSelector:@selector(scrollToBottom:) withObject:nil afterDelay:0.5];
    }
    
    - (void)scrollToBottom:(id)sender{
    
    // Scroll to bottom here
    
        var iPath = NSIndexPath(forRow: messagesArray.count - 1, inSection: 0)
    tableView.scrollToRowAtIndexPath(iPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-07
      • 2022-12-23
      • 2011-08-26
      • 2023-03-22
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多