从底部swift start tableView(反向tableView)

时间:2014-10-01 18:44:39

标签: swift ios8 tableview xcode6

是否可以逆转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
      ^
      |
      |

感谢您的帮助

4 个答案:

答案 0 :(得分:3)

滚动到最后,无论何时插入单元格都一样。

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

答案 1 :(得分: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)

答案 2 :(得分:1)

找到

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

var messagesArray =字符串

self.messagesArray = self.messageArray.reversed()

聊天应用

enter image description here

答案 3 :(得分:0)

试试这个:

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

和单元格内部:

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