Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customized tableView à la Tweetbot

The one you see below is a screen shot of Tweetbot iPhone application. What I want to ask is if you know a way I can achieve what the Tapbots guys achieved in their tableView: you tap on a cell of the former tableView and it reveals a set of actions, tap again and it disappear.

enter image description here

It is a really cool looking implementation and I would love to know your opinion on how I can do that both for practice and also because I would love to use a variant of this in a future project.

Any idea?

like image 366
Francesco Avatar asked Feb 10 '12 11:02

Francesco


1 Answers

When the user selects the cell save the the index path of the selected cell and add a row below it. If the user selects the same sell hide the row below it. If the user selects another cell hide the currently selected cell and show a new cell below the newly selected cell.

@interface RCTableViewController ()

@property (nonatomic, strong) NSMutableArray *tableViewData;
@property (nonatomic, strong) NSIndexPath *selectedIndexPath;

@end

@implementation RCTableViewController

@synthesize tableViewData = _tableViewData;
@synthesize selectedIndexPath = _selectedIndexPath;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableViewData = [NSMutableArray arrayWithObjects:
                 @"Cell 0",
                 @"Cell 1",
                 @"Cell 2",
                 @"Cell 3",
                 @"Cell 4",
                 @"Cell 5",
                 @"Cell 6",
                 @"Cell 7",
                 @"Cell 8",
                 nil];
    self.selectedIndexPath = nil;
    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableViewData.count;
}

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

    if (indexPath == self.selectedIndexPath) {
        static NSString *DropDownCellIdentifier = @"DropDownCell";

        cell = [tableView dequeueReusableCellWithIdentifier:DropDownCellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DropDownCellIdentifier];
        }

        cell.textLabel.text = @"Drop Down Cell";

    } else {
        static NSString *DataCellIdentifier = @"DataCell";

        cell = [tableView dequeueReusableCellWithIdentifier:DataCellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DataCellIdentifier];
        }

        cell.textLabel.text = [self.tableViewData objectAtIndex:indexPath.row];
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *dropDownCellIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1
                                                            inSection:indexPath.section];

    if (!self.selectedIndexPath) {
        // Show Drop Down Cell        
        [self.tableViewData insertObject:@"Drop Down Cell" atIndex:dropDownCellIndexPath.row];

        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:dropDownCellIndexPath] 
                         withRowAnimation:UITableViewRowAnimationTop];        

        self.selectedIndexPath = indexPath;    
    } else {
        NSIndexPath *currentdropDownCellIndexPath = [NSIndexPath indexPathForRow:self.selectedIndexPath.row + 1
                                                                inSection:self.selectedIndexPath.section];

        if (indexPath.row == self.selectedIndexPath.row) {
            // Hide Drop Down Cell
            [self.tableViewData removeObjectAtIndex:currentdropDownCellIndexPath.row];

            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:currentdropDownCellIndexPath]
                             withRowAnimation:UITableViewRowAnimationTop];

            self.selectedIndexPath = nil;
        } else if (indexPath.row == currentdropDownCellIndexPath.row) {
            // Dropdown Cell Selected - No Action
            return;
        } else {
            // Switch Dropdown Cell Location     
            [tableView beginUpdates];
            // Hide Dropdown Cell
            [self.tableViewData removeObjectAtIndex:currentdropDownCellIndexPath.row];

            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:currentdropDownCellIndexPath]
                             withRowAnimation:UITableViewRowAnimationAutomatic];

            // Show Dropdown Cell            
            NSInteger dropDownCellRow = indexPath.row + ((indexPath.row >= currentdropDownCellIndexPath.row) ? 0 : 1);
            dropDownCellIndexPath = [NSIndexPath indexPathForRow:dropDownCellRow
                                                       inSection:indexPath.section];


            [self.tableViewData insertObject:@"Drop Down Cell" atIndex:dropDownCellIndexPath.row];

            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:dropDownCellIndexPath] 
                             withRowAnimation:UITableViewRowAnimationAutomatic];        

            self.selectedIndexPath = [NSIndexPath indexPathForRow:dropDownCellIndexPath.row - 1 
                                                        inSection:dropDownCellIndexPath.section];
            [tableView endUpdates];                            
        }        
    }
}

@end
like image 167
richerd Avatar answered Nov 06 '22 02:11

richerd