Tableview keeps going back to top? After leaving to "detailed" vc?

E

ezera.olsen

Guest
How can I prevent the view controller from starting from the top of the feed when a user leaves and goes back?

Basically, I have the main VC and a detailed VC. When the user selects a cell, it should jump to the detailed VC. If she/he goes back, it should leave her back to where she/he was.

I get that my code is calling "reload Data" every time the VC loads, but what other options do I have then if I don't call that method?

Here's an image of my main storyboard if it helps. Main VC(left) is the feed tableView where the user can tap on the cell. When he/she taps on the cell, it "segues" to the comment table VC (right). When he/she is done commenting she/he can go back to the main VC and continuing going down the feed. (ideally, except it keeps loading from the newest post, rather than segueing the user back to where she/he was down in the feed)

[enter image description here][1]

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)
})


[1]:
Continue reading...
 
Top