Подтвердить что ты не робот

Настроенный tableView à la Tweetbot

Ниже вы видите снимок экрана приложение iPhone Tweetbot. Я хочу спросить, знаю ли вы, каким образом я могу достичь того, чего достигли ребята Tapbots в их таблицеView: вы нажимаете на ячейку прежнего tableView и обнаруживаете набор действий, снова нажимаете и исчезаете.

enter image description here

Это действительно классная реализация, и я хотел бы узнать ваше мнение о том, как я могу это сделать как на практике, так и потому, что я хотел бы использовать вариант этого в будущем проекте.

Любая идея?

4b9b3361

Ответ 1

Когда пользователь выбирает ячейку, сохраните путь указателя к выбранной ячейке и добавьте строку под ней. Если пользователь выбирает одну и ту же сделку, спрячьте строку под ней. Если пользователь выбирает другую ячейку, скрыть выбранную ячейку и показать новую ячейку ниже выбранной ячейки.

@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