2015-04-01 91 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