iOS如何只扩展一个单元格

时间:2015-04-01 01:18:24

标签: ios cell expand

我最近尝试编写我的第一个应用程序,但我遇到了一个问题,我似乎无法找到解决方案。

我正在尝试扩展被点击的单元格高度。我遵循了这个link但是当我点击一个单元格时,其余的单元格也被扩展了。有谁知道为什么会这样?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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