1

I have recently try to write my very first app and I have encountered a problem that I seems not be able to find a solution.

I am trying to expand the cell height that's being tapped. I followed this link but when I clicked on one cell, the rest of the cells expanded too. Does anyone know why this is happening?

Community
  • 1
  • 1
sof
  • 11
  • 4
  • use this answer this will help you in what you want to do. In this you find the accordian library that is what you want to do. http://stackoverflow.com/questions/15442697/how-to-create-an-accordion-with-uitableview-under-a-uitableview – jogshardik Apr 01 '15 at 07:10
  • How I want this program to work is to show more information about that particular cell item when click on that, instead of displaying more cell. Would using accordian be better in this case? – sof Apr 02 '15 at 00:10

3 Answers3

1

Here is the simple implementation of your problem.

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSIndexPath *selectedIndexPath;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    selectedIndexPath = [NSIndexPath new];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ( indexPath == selectedIndexPath)
    {
        return 200;
    }
    else
    {
        return 90;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectedIndexPath = indexPath;
    [tableView reloadData];
}
@end
Prajeet Shrestha
  • 7,978
  • 3
  • 34
  • 63
0

The correct way is to use the datasource to everything related to specific cell's view. This is an example of implementation of click and expand cells. In fact for iOS developers this should be one of most often used patterns.

@interface MySitesViewController ()

@property (strong, nonatomic) NSMutableArray *menuRows;

@end

@implementation MySitesViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _menuRows = [NSMutableArray array];
    [_menuRows addObject:@{
                           @"title":"Some title",
                           @"type": @"MyCellType",
                           @"height":@(44)
                           }.mutableCopy];
    [_menuRows addObject:@{
                           @"title":"Some title",
                           @"type": @"MyCellType",
                           @"height":@(44)
                           }.mutableCopy];
    [_menuRows addObject:@{
                           @"title":"Some title",
                           @"type": @"MyCellType",
                           @"height":@(44)
                           }.mutableCopy];
    [_menuRows addObject:@{
                           @"title":"Some title",
                           @"type": @"MyCellType",
                           @"height":@(44)
                           }.mutableCopy];    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_menuRows count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *data = _menuRows[indexPath.row];
    return data[@"height"];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *data = _menuRows[indexPath.row];
    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:data[@"type"]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *data = _menuRows[indexPath.row];    
    [tableView deselectRowAtIndexPath:indexPath animated: YES];

    NSInteger height = ((NSNumber*)data[@"height"]).integerValue;
    if(height == 44)
    {
        data[@"height"] = @(200);
    }else
    {
        data[@"height"] = @(44);
    }

    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

@end
0
.h File
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
    NSIndexPath *selectedIndexPath;

    IBOutlet UITableView *tblExpandCell;
}
@end
.m File
#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    selectedIndexPath = [NSIndexPath new];
    [self.view  setBackgroundColor:[UIColor redColor]];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"newFriendCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    [cell setBackgroundColor:[UIColor redColor]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ( indexPath == selectedIndexPath)
    {
        return 200;
    }
    else
    {
        return 60;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectedIndexPath = indexPath;
    [tableView reloadData];
}
@end