I have a UITableView which contains comments on a particular film, I need to implement something like a comment comment, that is, there is a main comment from “Petit” and “Vladimir” can comment from the commentary “Petit”. How this can be implemented on iOS, I just don’t stupidly imagine this much, not knowing how the table works.

  • one
    This is not a table (list), but a tree. Look for implementations on github or cocoacontrols.com - AlexDenisov
  • Either I am blind, or I cannot find, I lead to what I was looking for, I did not find, so I decided to write here. - Zarochintsev
  • 2
    Just add an extra UITableViewCell, what's the problem? return from numberOfRowsInSection: (NSINteger) section comments.count + subcomments.count, everything depends on the level of comments nesting, if the level is only one - then the task is not very difficult - iFreeman

1 answer 1

First, you need to properly organize the storage of data (comments). Comments, discussions are trees, in this example the root of the tree is a movie, therefore you need to know how to implement trees. This is usually an array of branches (a branch in the context of this task is an object of the Comment class). To organize a tree structure of comments, each comment must have attributes:

Comment * parent; NSArray * childs; 

that is, a link to the parent comment and an array of child comments. I think that it is not necessary to explain the difference between the parent and child comments?

Secondly, you need to implement a minimum set of methods for working with the comment tree: linearization into a coherent list, inserting a comment into the tree (commenting). Linearization to the list is needed in order to convert a deep multi-level tree structure into a flat one-level list. Simply put, to have a single array, in which all the comments will be arranged in a suitable order, for example: comment 1, comment 1.1, comment 1.1.1, comment 1.2, comment 2, comment 2.1 and so on. Further this list can be used as a data source for UITableView, each cell will coincide with the corresponding comment. If we just write comments to the film, then simply insert it into the array of our comments. If we comment on someone else's comment, it may look like this:

 @interface Comment : NSobject ... -(void)addComment:(NSString *)text; ... @implementation Comment -(void)addComment:(NSString *)text { Comment * newComment = [[Comment alloc] initWithText:text]; self.childs = [self.childs arrayByAddingObject:newComment]; } 

After each comment is added, you need to re-linearize the entire tree so that the new comment fits into place (under the most recent comment in a particular comment thread). In general, this topic is too voluminous, but in my answer I tried to indicate which way to google.