Estoy creando una aplicación que tiene una vista de noticias en tiempo real para publicaciones enviadas por usuarios. Esta vista tiene una implementación UITableViewpersonalizada UITableViewCell. Dentro de esta celda, tengo otra UITableViewpara mostrar comentarios. La esencia es algo como esto:
Feed TableView
PostCell
Comments (TableView)
CommentCell
PostCell
Comments (TableView)
CommentCell
CommentCell
CommentCell
CommentCell
CommentCell
La fuente inicial se descargará con 3 comentarios para obtener una vista previa, pero si hay más comentarios, o si el usuario agrega o elimina un comentario, quiero actualizar el contenido PostCelldentro de la vista de la tabla de fuentes agregando o eliminando CommentCellsla tabla de comentarios en el interior. del PostCell. Actualmente estoy usando el siguiente ayudante para lograr eso:
// (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's 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
}
Esto logra la actualización muy bien. La publicación se actualiza en su lugar con comentarios agregados o eliminados dentro del PostCellcomo se esperaba. Estoy usando el tamaño automático PostCellsen la mesa de alimentación. La tabla de comentarios se PostCellexpande para mostrar todos los comentarios, pero la animación es un poco desigual y la tabla se desplaza hacia arriba y hacia abajo una docena de píxeles aproximadamente mientras se lleva a cabo la animación de actualización de la celda.
El salto durante el cambio de tamaño es un poco molesto, pero mi problema principal viene después. Ahora, si me desplazo hacia abajo en la fuente, el desplazamiento es suave como antes, pero si me desplazo hacia arriba por encima de la celda que acabo de cambiar de tamaño después de agregar comentarios, la fuente saltará hacia atrás unas cuantas veces antes de llegar a la parte superior de la fuente. Configuré iOS8celdas de tamaño automático para el Feed de esta manera:
// (FeedController.swift)
// tableView is the feed table containing PostCells
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 560
Si elimino el estimatedRowHeight, la tabla simplemente se desplaza hacia la parte superior cada vez que cambia la altura de una celda. Me siento bastante atrapado en esto ahora y, como nuevo desarrollador de iOS, podría usar cualquier consejo que pueda tener.