3

I have subclassed a UITableViewCell. What I want to have is when I click on the cell, I added a UIView to the bottom of the cell and adjust the height of the cell accordingly. Here's my code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{   
MyCell* cell = (MyCell *) [self.table cellForRowAtIndexPath:indexPath]; 
UIView * options = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height - 27, cell.frame.size.width, 27)];
UIButton* button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[options addSubview:button1];
UIButton* button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[options addSubview:button2];
UIButton* button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[options addSubview:button3];
//did some layout calculation here to position the button
[cell addSubview:options];
}

Here's a video that illustrates the issue

Also I've tried to change the accessoryView of my cell from my custom UIButton to the regular UITableViewCellAccessoryDisclosure but it didn't work

MyCell * cell = (MyCell *)[self.table cellForRowAtIndexPath:temp];
     [cell.accessoryView removeFromSuperview];
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

If you're curious what I am trying to do, I am trying to create a tweetbot uitableviewcell, where you press the cell/row and it presents you with other options (retweets, etc) and also it was shown animated.

3

2 Answers 2

2

I don't know if I correctly understand what you are trying to do. But if you do want to animate the change of the UITableViewCell's height try to adjust it within - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath and call

[tableView beginUpdates];
[tableView endUpdates];
2
  • I think if you look at the video you'd understand. is the heightForRowAtIndexPath delegate going to be called after I do the addSubview? May 15, 2011 at 18:22
  • Actually I watched your video. No this delegate is not called by simply adding a subview. But it gets in fact called when you use those beginUpdates and endUpdates methods. (Look at my answer) Simply include them when adding your subviews.
    – fscheidl
    May 15, 2011 at 20:16
0

Put controls into another cell and use

insertRowsAtIndexPaths:withRowAnimation:   

to show it and

deleteRowsAtIndexPaths:withRowAnimation: 

to hide it.

2
  • I thought of this approach as well, however my UITable data source is from an NSArray of Message, this means that I would need to add something that is not a Message to the data source? Is this fine? I would also then need to modify the cellForRowAtIndexPath to handle this special case, correct? May 15, 2011 at 19:38
  • Basically, yes. Whenever controls are not hidden your tableView:numberOfRowsInSection: should return number_of_items + 1 and tableView:cellForRowAtIndexPath: should return the appropriate cell for controls. May 15, 2011 at 20:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.