2015-04-01 2 views
1

Недавно я попытался написать свое первое приложение, и я столкнулся с проблемой, которая, кажется, не в состоянии найти решение.iOS Как расширить только одну ячейку

Я пытаюсь расширить высоту ячейки, которая используется. Я последовал за этим link, но когда я нажал на одну ячейку, остальные ячейки тоже расширились. Кто-нибудь знает, почему это происходит?

+0

использовать этот ответ, это поможет вам в том, что вы хотите сделать. В этом вы найдете гармоничную библиотеку, которую вы хотите сделать. http://stackoverflow.com/questions/15442697/how-to-create-an-accordion-with-uitableview-under-a-uitableview – jogshardik

+0

Как я хочу, чтобы эта программа работала, чтобы показать больше информации об этой конкретной ячейке когда вы нажимаете на него, вместо того, чтобы отображать больше ячейки. Будет ли использование гармоник лучше в этом случае? – sof

ответ

1

Вот простая реализация вашей проблемы.

#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 
0

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

@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 
Смежные вопросы