首页 > 解决方案 > Tableview 不断回到顶部?离开后去“详细”的vc?

问题描述

当用户离开和返回时,如何防止视图控制器从提要的顶部开始?

基本上,我有主要的VC和详细的VC。当用户选择一个单元格时,它应该跳转到详细的 VC。如果她/他回去,它应该让她回到她/他所在的地方。

我知道每次加载 VC 时我的代码都会调用“重新加载数据”,但是如果我不调用该方法,我还有哪些其他选择?

如果有帮助,这是我的主要故事板的图像。主 VC(左)是用户可以点击单元格的 feed tableView。当他/她点击单元格时,它“继续”到评论表 VC(右)。当他/她完成评论后,她/他可以返回主 VC 并继续向下查看提要。(理想情况下,除了它不断从最新帖子加载,而不是让用户回到她/他在提要中的位置)

在此处输入图像描述

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostCell
        
        let post: PostModel

        
        post = postList[indexPath.row]
        
        func set(post: PostModel) {
            ImageService.downloadImage(withURL: post.author.patthToImage) { image in
                cell.profileImage.image = image
            }
        }
        set(post: postList[indexPath.row])
        cell.descriptionLabel.numberOfLines = 0 // line wrap
        cell.descriptionLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
        
        cell.descriptionLabel.text = post.message

        cell.authorLabel.text = post.author.username

        cell.timeLabel.text = post.createdAt.calendarTimeSinceNow()
        
        //takes care of post image hidding and showing
        if self.postList[indexPath.row].pathToImage != "" {
            cell.postImage.isHidden = false
            cell.postImage?.downloadImage(from: self.postList[indexPath.row].pathToImage)
        
        } else {
            cell.postImage.isHidden = true

        }
        
        if cell.postImage.isHidden == true {
            cell.postImage.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
            
        }

        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let post: PostModel
        
        post = postList[indexPath.row]
        myIndex = indexPath.row
        myPost = post.postID!
        performSegue(withIdentifier: "segue", sender: self)
        print(myIndex)
        print(post.postID)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        beginBatchFetch()
        
    }

    func beginBatchFetch() {
        fetchingMore = true

        fetchPosts { newPosts in
            self.postList.append(contentsOf: newPosts)

            self.endReached = newPosts.count == 0
            self.fetchingMore = false
            self.tableViewPost.reloadData()
 
        }

    }
    func fetchPosts(completion: @escaping(_ postList:[PostModel])->()) {
        ref = Database.database().reference().child("posts")
        var queryRef:DatabaseQuery
        let lastPost = self.postList.last
        
        if lastPost != nil {
            let lastTimestamp = lastPost!.createdAt.timeIntervalSince1970 * 1000
            queryRef = ref.queryOrdered(byChild: "timestamp").queryEnding(atValue: lastTimestamp).queryLimited(toLast:20)
        } else {
            queryRef = ref.queryOrdered(byChild: "timestamp").queryLimited(toLast:20)
        }
        
        queryRef.observeSingleEvent(of: .value, with: { snapshot in

            var tempPosts = [PostModel]()


            for child in snapshot.children {
                if let childSnapshot = child as? DataSnapshot,
                    let dict = childSnapshot.value as? [String:Any],
                    let author = dict["author"] as? [String:Any],
                    let uid = author["uid"] as? String,
                    let username = author["username"] as? String,
                    let fullname = author["fullname"] as? String,
                    let patthToImage = author["patthToImage"] as? String,
                    let url = URL(string:patthToImage),

                    let pathToImage = dict["pathToImage"] as? String,
                    let likes = dict["likes"] as? Int,
                    let postID = dict["postID"] as? String,
                    let message = dict["message"] as? String,
                    let genre = dict["genre"] as? String,
                    let timestamp = dict["timestamp"] as? Double {


                    let userProfile = UserProfile(uid: uid, fullname: fullname, username: username, patthToImage: url)
                    let post = PostModel(genre: genre, likes: likes, message: message, pathToImage: pathToImage, postID: postID, userID: pathToImage, timestamp: timestamp, id: childSnapshot.key, author: userProfile)
                    tempPosts.insert(post, at: 0)
                }
            }

            //first two
            self.postList = tempPosts
            self.tableViewPost.reloadData()
//            return completion(tempPosts)
        })

标签: swift

解决方案


“我添加了一个“返回”按钮来继续返回”这就是问题所在。返回的方法是调用dismiss——不要使用第二个转场(除非它是一个特殊的“展开”转场,但你不知道该怎么做)。


推荐阅读