views:

171

answers:

2

Hi all,

In my application I have this requirement that first tap on custom cell of uitableview with a label in it should expand it and second should contract it. I'm able to expand and contract cell and expand label inside cell, but not able to contract the label on second tap.

I'm using this function

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

[super setSelected:selected animated:animated];
if( selected == YES ) {
    [self expandRow];
}
else {
    [self contractRow];
}

height = [lblFeed frame].size.height + 75;
}

expandRow expands the label and contractRow contracts it. I'm perplexed as for how many rows this function gets called. It doesn't get called only for the cell tapped, it gets called more number of times for single tap on single cell may be for other cells but I'm not getting which rows.

This' really urgent.

Can anybody please help?

A: 

I would suggest that you don't add your functionality on top of the selected property of the cell, which has slightly different behaviour than you expect.

Just add your own BOOL expanded property, and see how that works. You should probably call it from the UITableView delegate methods, too.

Paul Lynch
+1  A: 

Tapping a selected row doesn't cause it to be deselected. When a cell gets selected, it stays selected until deselectRowAtIndexPath:animated: gets called on its table. That's why your method isn't getting called for the second tap.

In an MVC architecture like UIKit, it's recommended that you handle user interactions in your controller classes. It would be appropriate to override -[UITableViewCell setSelected:animated:] if all you were doing was customizing the way the view represents a selected cell, but in this case your expand/contract toggle behavior would require a change in the way UITableView selects and deselects its cells.

You could subclass UITableView and implement this toggle behavior yourself, or you can leave UITableView alone and handle it all at the UIViewController level by doing something like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.expandedIndexPath isEqual:indexPath]) {
        [(YourCustomCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath] contractRow];
        self.expandedIndexPath = nil;
    }
    else {
        if (self.expandedIndexPath) {
            [(YourCustomCell *)[self tableView:tableView cellForRowAtIndexPath:self.expandedIndexPath] contractRow];
        }
        [(YourCustomCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath] expandRow];
        self.expandedIndexPath = indexPath;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
cduhn
Thanx cduhn.. It helped..
neha
But what do you mean by 'subclass UITableView', I already have a class which subclasses uitableview and handles all my tableview behaviour. I should include this in that class itself or create a new class? Also how can I handle this in uiviewcontroller as this is tableView delegate method? Thanx..
neha
If you're already subclassing UITableView, then you could implement code that deselects the selected cell when the user touches it. That's the piece you're missing. When I said UIViewController I should have said UITableViewController, which implements UITableViewDelegate and UITableViewDataSource. If you're using some other class as your UITableViewDelegate you could implement the same thing there.
cduhn