I am creating an application that has a feed view for user posts. This view has a UITableView with a custom implementation of UITableViewCell . Inside this cell, I have another UITableView to display comments. The bottom line is:
Feed TableView PostCell Comments (TableView) CommentCell PostCell Comments (TableView) CommentCell CommentCell CommentCell CommentCell CommentCell
The original feed will be loaded with 3 comments for preview, but if there are more comments or if the user adds or removes a comment, I want to update PostCell in place inside the feed table view by adding or removing CommentCells to the comment table inside PostCell . I am currently using the following helper to accomplish this:
// (PostCell.swift) Handle showing/hiding comments func animateAddOrDeleteComments(startRow: Int, endRow: Int, operation: CellOperation) { let table = self.superview?.superview as UITableView // "table" is outer feed table // self is the PostCell that is updating it comments // self.comments is UITableView for displaying comments inside of the PostCell table.beginUpdates() self.comments.beginUpdates() // This function handles inserting/removing/reloading a range of comments // so we build out an array of index paths for each row that needs updating var indexPaths = [NSIndexPath]() for var index = startRow; index <= endRow; index++ { indexPaths.append(NSIndexPath(forRow: index, inSection: 0)) } switch operation { case .INSERT: self.comments.insertRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.None) case .DELETE: self.comments.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.None) case .RELOAD: self.comments.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.None) } self.comments.endUpdates() table.endUpdates() // trigger a call to updateConstraints so that we can update the height constraint // of the comments table to fit all of the comments self.setNeedsUpdateConstraints() } override func updateConstraints() { super.updateConstraints() self.commentsHeight.constant = self.comments.sizeThatFits(UILayoutFittingCompressedSize).height }
This is great for updating. The post is updated in-place with comments added or deleted inside PostCell as expected. I use PostCells auto- PostCells in the feed table. The PostCell comment PostCell expands to display all the comments, but the animation is a bit jerky, and the table sorts the scroll up and down by a dozen pixels or so while the cell update animation is happening.
Leaping when resizing is a bit annoying, but my main problem comes later. Now, if I scroll down in the feed, the scroll will be as smooth as before, but if I scroll up above the cell, I just resized after adding comments, the feed jumps back several times before it reaches the top of the feed. I configure iOS8 automatic iOS8 cells for Feed as follows:
If I delete the estimatedRowHeight , the table simply scrolls to the beginning anytime the cell height changes. I feel pretty stuck on this now and as a new iOS developer, I could use whatever advice you might have.
ios uitableview ios8 swift
Bryan Alger Jan 17 '15 at 5:22 2015-01-17 05:22
source share