2015-04-01 86 views
1

我最近尝试编写我的第一个应用程序,我遇到了一个问题,我似乎无法找到解决方案。iOS如何仅扩展一个单元

我想扩大被挖掘的细胞高度。我跟着这个link,但当我点击一个单元时,其余的单元也扩展了。有谁知道为什么会发生这种情况?

+0

利用这个答案,这将帮助你在你想要做什么。 在这里你可以找到你想要做的手风琴图书馆。 http://stackoverflow.com/questions/15442697/how-to-create-an-accordion-with-uitableview-under-a-uitableview – jogshardik 2015-04-01 07:10:39

+0

我想让这个程序工作是为了显示有关特定单元格的更多信息项目点击时,而不是显示更多的单元格。在这种情况下使用手风琴会更好吗? – sof 2015-04-02 00:10:45

回答

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