【问题标题】:swift start tableView from bottom (reverse tableView)从底部快速启动tableView(反向tableView)
【发布时间】:2014-10-01 18:44:39
【问题描述】:

tableView 可以倒序排列吗?我搜索了很多解决方案,但没有任何效果。

效果就像whatsapp聊天。

一个普通的tableView是:

----------
label 1
----------
label 2
----------
label 3
----------
empty
----------
empty
----------

scroll direction
   |
   |
   v

想要的结果:

----------
empty
----------
empty
----------
empty
----------
empty
----------
label 3
----------
label 2
----------
label 1
----------

 scroll direction
      ^
      |
      |

感谢您的帮助

【问题讨论】:

  • cell.setTitle.text = myArray.reversed()[indexPath.row] 反转你的数组就可以了。

标签: swift ios8 tableview xcode6


【解决方案1】:

只要滚动到末尾,每次插入单元格时都这样做。

tableView.scrollToRowAtIndexPath(bottomIndexPath, atScrollPosition: .Bottom,
          animated: true)

【讨论】:

  • 滚动到最后不要颠倒单元格的顺序:(
  • 我以为你已经这样做了。只需在cellForRowAtIndexPath() 中返回正确的单元格。
  • 我再试一次,但还是不行。首先我反转tableview的数据,然后我用第一个indexpath设置一个var,然后我用scrollToRowAtIndexPath调用一个func,但tableview状态再次位于顶部。我再试一次..谢谢!
【解决方案2】:

为了补充上面的答案,我提供了使用 Parse 加载、加载更多和刷新的方法。或者你可以用你喜欢的任何其他获取方法替换 Parse。

首次加载

    var query = PFQuery(className: "Comments")
    query.orderByDescending("createdAt")
    // limit the number of objects
    query.limit = objectsPerPage
    query.findObjectsInBackgroundWithBlock { (comments: [AnyObject]?, error: NSError?) -> Void in
        // clean array if loading for the first time
        self.objects.removeAll()
        for eachComment in comments!
            if self.objects.count < self.objectsPerPage
            {
                self.objects.append(eachComment as! PFObject)
            }
        }

        // reverse the array
        self.objects = self.objects.reverse()

        self.tableView.reloadData()
        self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.objects.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: false)

加载更多

    // for the query
    //skip already loaded objects
    query.skip = self.objects.count; 
    // limit the number of objects
    query.limit = objectsPerPage
    // find the number of objects already loaded
    let numberOfAlreadyLoadedObjects = self.objects.count

    // add your  query.findObjects here

    // correct the order to update the array in the right order
    self.objects = self.objects.reverse()

    // add new posts
    for eachComment in comments!
            if (self.objects.count < self.objectsPerPage + numberOfAlreadyLoadedObjects)
            {
                self.objects.append(eachComment as! PFObject)
            }
    }

    // reverse again
    self.tableView.reloadData()

    let lastIndex = NSIndexPath(forRow: self.objectsPerPage - 2 , inSection: 0)
    self.tableView.scrollToRowAtIndexPath(lastIndex, atScrollPosition: UITableViewScrollPosition.Top, animated: false)

刷新

    // skip already loaded objects
    let numberOfAlreadyLoadedObjects = self.objects.count
    query.skip = numberOfAlreadyLoadedObjects

    // add your  query.findObjects here

    // correct the order
    self.objects = self.objects.reverse()

    // count the number of new objects found
    let newObjectsFound = comments?.count
    // append just the new ones
    for eachComment in comments!
    {
            if self.objects.count < numberOfAlreadyLoadedObjects + newObjectsFound!
            {
                self.objects.append(eachComment as! PFObject)
            }
     }
     // reverse
     self.objects = self.objects.reverse()

     self.tableView.reloadData()
     let lastIndex = NSIndexPath(forRow: self.objects.count - 1, inSection: 0)
     self.tableView.scrollToRowAtIndexPath(lastIndex, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)

【讨论】:

    【解决方案3】:

    找到 Al-Hamdulellah 解决方案 只反转你的数组并像这样加载它:

    var messagesArray = 字符串

    self.messagesArray = self.messageArray.reversed()

    聊天应用

    【讨论】:

      【解决方案4】:

      试试这个:

      tableView.transform = CGAffineTransform(scaleX: 1, y: -1)
      

      和细胞内:

      transform = CGAffineTransform(scaleX: 1, y: -1)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-18
        • 2016-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-30
        相关资源
        最近更新 更多