0

enter image description here![enter image description here][2]I have a table view,in which I am using my custom cell that contains user's profile picture,status label, time stamp label, username label, no of comments label and a comment button. When no of comments i.e"4 comments" is tapped, I ha ve to show comments in the corresponding cell along with username, user profile picture etc below the original post. How do I dynamically add these objects into cell when the app is running? or there is some else solution please tell. table for 2 posts is shown in right of image. Tapping no of comments left of the image should be shown. Here is the code for table cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



    static NSString *CellIdentifier = @"MyCell";
    TableCell *cell = (TableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self   options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (TableCell *)currentObject;
                break;
            }
        }
    }

    // Configure the cell.
    User *user = [postTableData objectAtIndex:indexPath.row];
    NSLog(@"pic:%@",user.profilePictureURL);
    cell.profilePicture.image = [UIImage imageNamed:@"avatar.png"];
    cell.profilePicture.layer.cornerRadius = cell.profilePicture.frame.size.height /2;
    cell.profilePicture.layer.masksToBounds = YES;
    cell.profilePicture.layer.borderWidth = 0;

    cell.userName.text = user.name;
    cell.status.text = user.status;
    cell.dateForStatus.text = user.datePosted;
    [cell.countComments setTitle:user.countOfComments forState:UIControlStateNormal];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.contentView.layer setBorderWidth:6.0f];
    [cell.contentView.layer setBorderColor:[[UIColor alloc] initWithRed:233/255.0 green:236/255.0 blue:233/255.0 alpha:1.0].CGColor];
    cell.commentButton.tag=indexPath.row;
    [cell.commentButton addTarget:self action:@selector(commentButtonPressAction:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
    ![enter image description here][3]}
2

4 Answers 4

1

I guess you are trying to expand the cell.

You can call these few lines to update the cell, including the height.

[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];

So just set a flag to show comments in cellForRowAtIndexPath.

//AFTER_YOUR_SETTING
if(showComment){
    [cell loadCommentMethod];
}

And also, in heightForRowAtIndexPath

if(showComment){
    return [CellClass publicMethodForCalHeight:dataAtIndexPath];
} else {
    return kDefaultCellHeight;
}
Sign up to request clarification or add additional context in comments.

2 Comments

this one looks closer,
how do i get indexpath?? I have indexpath.row
1

Change your button action method:

- (void)onButtonTaped:(id)sender
{
    UIButton *button = sender;
    UILabel *newLabel = [ [ UILabel alloc] initWithFrame:CGRectMake (0, 100, 200, 50)];
    newLabel.text = @"Your new text here";
    [button.superview addSubview:newLabel];
}

But label will be visible only until cell will be visible on screen. When cell will be recreated label will be deleted. To keep label you still need to change

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

1 Comment

This method is called when the tableview is loaded first time, not when label inside the table cell is tapped.
1

I'm not sure if it works but you can try this. Override touchesBegan: on your cell:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     UITouch *touch = [touches anyObject];
     CGPoint point = [touch locationInView:self];    
     CGRect rect = [noComments frame];

     if (CGRectContainsPoint(rect, point))
     {
        UILabel *label = ... // the label you need to add.
       [self addSubview:label];
     }
}

3 Comments

This one is a little fine. Problem is, it adds object when whole cell is tapped from any where in the cell.
@AliHassan you need to add objects when a certain area in the cell is tapped?
yup,when no of comments is tapped
0

You can make use of the method insertRowsAtIndexPaths:withRowAnimation: of the UITableView. You can refer this link for a tutorial. Also refer the Apple docs

2 Comments

This will add new cell. I need to objects in the cell. is this possible?
Actually I would suggest, instead of adding objects (I'm not sure what objects you are referring to) add cells itself. Create a new custom cell and insert cells as many comments you have. You can also provide decent animation during the show/hide comment process which are readily available in the form of UITableViewRowAnimations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.