A
A
Alexander Zarochintsev2014-03-20 10:36:44
Objective-C
Alexander Zarochintsev, 2014-03-20 10:36:44

UITableView - how to attach comments to posts?

Good time of the day! There was a problem, in general, the request receives information from the server with the structure:

{
     /* 1 */
     "first_name" : "Alex",
     "last_name" : "Kovalskiy",
     "last_activity" : "2014-03-14 14:03:07" ,
     "photo" : "http://",
     
     /* 2 */
     "comments" : [{
          "id" : "1',
          "message" : "Hello World",
          "profile" : {}
     },
     {
          "id" : "2',
          "message" : "Hello People",
          "profile" : {}
     },
] }

The information needs to be displayed in UITableView, where:
/* 1 */ - Information about the user;
/* 2 */ - Information about user comments
The essence of the problem:
There is a table with a list of users in one cell, under each of the users a list of comments to the user, that is:
- пользователь 1
     - комментарий 1
     - комментарий 2
     - комментарий 3
- пользователь 2
     - комментарий 1
     - комментарий 2
- пользователь 3
     - комментарий 1
....

I understand that in principle, my user is basically a section, and user comments are lines in the section, but I don’t quite understand how this can be implemented, please share your ideas, I will be very grateful for your help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Morozov, 2014-03-20
@Kovalskiy

I would make two types of cells with different designs, meaning the first type cell represents the user and the second type cell represents the comment. accordingly, they have different indents, different heights, etc.
if you need cells to open / close and / or display only, for example, the last 3 comments, then by clicking on the "open comments" button, call the reloadData method and let it rebuild the visible part of the table
UPD:
You need to create two different cells in the storyboard and assign them in the settings there are two different ReuseIdentifiers (for example, UserCell for a user cell and CommentCell for a comment cell). Then register delegates for the number of sections and the number of cells in each section:

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.users.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.users[section].comments.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (indexPath.row == 0)
  {
    UITableViewCell *cell = [[self tableViewAllCheckins] dequeueReusableCellWithIdentifier:@"UserCell"];
    
    NSDictionary *userInfo = self.users[indexPath.section];
    
    // заполнение ячейки User-а
    
    return cell;
  }
  else
  {
    CommentViewCell *commentCell = [[self tableViewAllCheckins] dequeueReusableCellWithIdentifier:@"CommentCell"];
    
    UserInfo *userInfo = self.users[indexPath.section];
    
    CommentInfo *comment = userInfo.comments[indexPath.row - 1]; // так как нулевая ячейка в секции принадлежит пользователю, а первая ячейка принадлежит нулевому комментарию, то надо сделать минус 1
    
    // заполнение ячейки Comment-а
    
    return commentCell;
  }
}

M
MagoVinch, 2014-03-20
@MagoVinch

Here is www.imaladec.com/story/dropdowntable

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question