我需要在ios 8中实现可扩展的tableView单元 [英] I need to implement the expandable tableView cell in ios 8

查看:100
本文介绍了我需要在ios 8中实现可扩展的tableView单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我需要实现UITableview,其中一些tableView单元是可扩展的,其中一些是独立的。如果是可扩展细胞,则需要在此处指示'+'符号。中的图像描述。任何人都可以帮帮我

In my project I need to implement the UITableview with some of the tableView cells are expandable and some of them are independent. If it is expandable cell need to indicate the '+' symbol.enter image description here. Can any one please help me out

推荐答案

我创建了一个小型演示,

I have created a small demo,

https://github.com/haripalwagh/ExpandableTableviewCell

屏幕截图1:扩展单元格之前
< img src =https://i.stack.imgur.com/Pvrw3.pngalt =在此处输入图像说明>

Screenshot 1 : Before expanding a cell

屏幕截图2:扩展后cell

Screenshot 2 : After expanding a cell

@interface ViewController ()
<UITableViewDataSource,
UITableViewDelegate>
{
    UITableView *tblView;

    NSArray *cell0SubMenuItemsArray;

    BOOL isSection0Cell0Expanded;
}

@end

@implementation ViewController

# pragma mark - View Life Cycle

- (void)viewDidLoad
{
    [super viewDidLoad];


    tblView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    tblView.backgroundColor = [UIColor clearColor];
    tblView.delegate = self;
    tblView.dataSource = self;
    tblView.allowsSelection = YES;
    tblView.scrollEnabled = YES;
    tblView.alwaysBounceVertical = YES;
    [self.view addSubview:tblView];

    cell0SubMenuItemsArray = @[@"First Static Menu Item", @"Second Static Menu Item", @"Third Static Menu Item"];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self.view setNeedsLayout];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    [self updateViewDimensions];
}

- (void)updateViewDimensions
{
    tblView.frame = CGRectMake(0, 40, 320, 550);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

# pragma mark - UITableView Delegate and Datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        int cellCount = 2; // Default count - if not a single cell is expanded

        if (isSection0Cell0Expanded)
        {
            cellCount += [cell0SubMenuItemsArray count];
        }

        return cellCount;
    }

    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *strCellId = @"CellId";

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strCellId];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
            cell.textLabel.text = @"Expandable Cell";

            UIImageView *accessoryImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

            if (isSection0Cell0Expanded) // Set accessory view according to cell state - EXPANDED / NOT EXPANDED
            {
            accessoryImageView.image = [UIImage imageNamed:@"Minus.png"];
            cell.detailTextLabel.text = @"Status : Expanded";
        }
        else
        {
            accessoryImageView.image = [UIImage imageNamed:@"Plus.png"];
            cell.detailTextLabel.text = @"Status : Not Expanded";
        }

            cell.accessoryView = accessoryImageView;
        }
        else
        {
            if (isSection0Cell0Expanded && [cell0SubMenuItemsArray count] >= indexPath.row) // Check Expanded status and do the necessary changes
            {
                cell.textLabel.text = [NSString stringWithFormat:@"%@", [cell0SubMenuItemsArray objectAtIndex:indexPath.row - 1]];
            }
            else
            {
                cell.textLabel.text = @"Static Cell";
            }
        }
    }

    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        // Change status of a cell reload table

        isSection0Cell0Expanded = !isSection0Cell0Expanded;
        [tblView reloadData];
    }
}

您必须为每个可扩展单元格进行这样的管理。
希望这会对你有帮助..

You have to manage like this for every expandable cell. Hope this will help you..

这篇关于我需要在ios 8中实现可扩展的tableView单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆