Answer the question
In order to leave comments, you need to log in
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" : {}
},
] }
- пользователь 1
- комментарий 1
- комментарий 2
- комментарий 3
- пользователь 2
- комментарий 1
- комментарий 2
- пользователь 3
- комментарий 1
....
Answer the question
In order to leave comments, you need to log in
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;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question