Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a subview to a UITableViewCell and resize it while in editing mode?

Tags:

Edit: I'm starting a bounty on this one, as I'm looking for a "cleaner" solution with a UITableViewCell subclass, rather than messing with the UITableView delegate and datasource. Ideally, I'd like to see how could I also handle the UISlider events. Thanks!

I am using a group-styled UITableView and I have several cells that look like this:

enter image description here

Now, while I'm in editing mode, I'd like to have a UISlider that sets the value of the right UILabel, so that the result would look like this:

enter image description here

Could someone point me to the right direction? I'm not even sure if I should do that by subclassing UITableViewCell or in IB. Code snippets would be highly appreciated :)

like image 586
phi Avatar asked May 26 '11 11:05

phi


1 Answers

Being a sucker for bounties, i sat down and experimented a bit. This is what i have found to be the cleanest solution :

You will make a custom cell class. As i read from your post, you have no problem with this, and I belive it really is the only way.

In the header file:

@interface TableCellSlider : UITableViewCell {     UISlider *cellSlider; } 

In the main file :

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];     if (self) {         cellSlider = [[UISlider alloc] initWithFrame:CGRectMake(200, 10, 100, 20)];         [self addSubview:cellSlider];         cellSlider.hidden = YES;     }      return self; }  - (void)didTransitionToState:(UITableViewCellStateMask)state {      if (self.editing) {         cellSlider.hidden = NO;     } else {         cellSlider.hidden = YES;     } }  - (float)getValue {     return cellSlider.value; } 

As for the table and the cell height, add this to the table script :

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {     [self.tableView beginUpdates];     [super setEditing:editing animated:YES];     [self.tableView endUpdates];     //[self.tableView reloadData]; }       - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath      {          if (self.editing) {             return 50;         } else {             return 30;         }     } 

This should do the trick. You can use the getValue on the cells, so you don't have to synthesize the sliders, and they will hide/reappear for whatever reason the cells become editable.

Best of luck with your project :]

like image 153
Nils Munch Avatar answered Oct 27 '22 00:10

Nils Munch