Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS如何仅扩展一个单元格_Ios_Cell_Expand - Fatal编程技术网

iOS如何仅扩展一个单元格

iOS如何仅扩展一个单元格,ios,cell,expand,Ios,Cell,Expand,我最近试着写我的第一个应用程序,我遇到了一个问题,我似乎无法找到解决方案 我正试图扩大被挖掘的细胞高度。我遵循了这一点,但当我点击一个单元格时,其余的单元格也展开了。有人知道为什么会发生这种情况吗?下面是解决问题的简单方法 #import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> { NSIndexPath *selectedIndex

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


我正试图扩大被挖掘的细胞高度。我遵循了这一点,但当我点击一个单元格时,其余的单元格也展开了。有人知道为什么会发生这种情况吗?

下面是解决问题的简单方法

#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
#导入“ViewController.h”
@界面视图控制器()
{
NSIndexPath*选择的索引路径;
}
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
selectedIndexPath=[NSIndexPath新建];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回10;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“MyCell”];
返回单元;
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径
{
if(indexPath==selectedIndexPath)
{
返回200;
}
其他的
{
返回90;
}
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
selectedIndexPath=indexPath;
[tableView重新加载数据];
}
@结束

正确的方法是将数据源用于与特定单元格视图相关的所有内容。这是一个实现单击并展开单元格的示例。 事实上,对于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
.h文件
#进口
@界面ViewController:UIViewController
{
NSIndexPath*选择的索引路径;
ibuitableview*tblExpandCell;
}
@结束
.m文件
#导入“ViewController.h”
@界面视图控制器()
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
selectedIndexPath=[NSIndexPath新建];
[self.view setBackgroundColor:[UIColor redColor]];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回10;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“newFriendCell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:CellIdentifier];
}
[单元格设置背景颜色:[UIColor redColor]];
返回单元;
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径
{
if(indexPath==selectedIndexPath)
{
返回200;
}
其他的
{
返回60;
}
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
selectedIndexPath=indexPath;
[tableView重新加载数据];
}
@结束

使用此答案这将帮助您完成您想做的事情。在这里,你可以找到你想要做的手风琴库。我希望这个程序的工作方式是在单击该单元格项时显示有关该单元格项的更多信息,而不是显示更多单元格项。在这种情况下,使用手风琴会更好吗?它如何扩展单元格?
.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