Skip to content

Instantly share code, notes, and snippets.

@nielswh
Created December 4, 2012 23:29
Show Gist options
  • Select an option

  • Save nielswh/4210238 to your computer and use it in GitHub Desktop.

Select an option

Save nielswh/4210238 to your computer and use it in GitHub Desktop.
Creating a dropdown Utility for TableViewCell
//
// TweetbotViewController.m
// testingTweetbotCell
//
// Created by Niels Hansen on 12-12-04.
// Copyright (c) 2012 Niels Hansen. All rights reserved.
//
#import "TweetbotViewController.h"
#import "TweetbotInfoCell.h"
@interface TweetbotViewController () {
int selectedUtilityRow;
}
@property (nonatomic, strong) NSMutableArray *tableViewData;
@property (nonatomic, strong) NSIndexPath *selectedIndexPath;
@end
@implementation TweetbotViewController
@synthesize tableViewData = _tableViewData;
@synthesize selectedIndexPath = _selectedIndexPath;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
selectedUtilityRow = -1;
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];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tableViewData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == selectedUtilityRow && indexPath.section == self.selectedIndexPath.section) {
TweetbotInfoCell *utilityCell = (TweetbotInfoCell*)[tableView dequeueReusableCellWithIdentifier:@"DropdownCell"];
utilityCell.textLabel.text = @"Drop Down Cell";
return utilityCell;
} else {
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:@"DataCell"];
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier:@"DataCell"];
}
cell.textLabel.text = [self.tableViewData objectAtIndex:indexPath.row];
return cell;
}
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == selectedUtilityRow && self.selectedIndexPath.section == indexPath.section) {
[cell setBackgroundColor:[UIColor purpleColor]];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == selectedUtilityRow && self.selectedIndexPath.section == indexPath.section)
return 35;
else
return 66;
}
- (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 Celler" atIndex:dropDownCellIndexPath.row];
self.selectedIndexPath = indexPath;
selectedUtilityRow = dropDownCellIndexPath.row;
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:dropDownCellIndexPath]
withRowAnimation:UITableViewRowAnimationTop];
} 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];
selectedUtilityRow = -1;
[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];
selectedUtilityRow = -1;
// 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 Celler 2" atIndex:dropDownCellIndexPath.row];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:dropDownCellIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
self.selectedIndexPath = [NSIndexPath indexPathForRow:dropDownCellIndexPath.row - 1
inSection:dropDownCellIndexPath.section];
selectedUtilityRow = dropDownCellIndexPath.row;
[tableView endUpdates];
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment