我需要在ios 8中实现可扩展的tableView单元

时间:2015-07-02 07:24:37

标签: objective-c ios8

在我的项目中,我需要实现UITableview,其中一些tableView单元是可扩展的,其中一些是独立的。如果是可扩展的细胞,则需要指出“' +' symbol.enter这里的图像描述。任何人都可以帮助我enter image description here

2 个答案:

答案 0 :(得分:4)

我创建了一个小型演示,

https://github.com/haripalwagh/ExpandableTableviewCell

屏幕截图1:扩展单元格之前 enter image description here

屏幕截图2:扩展单元格后 enter image description here

@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];
    }
}

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

答案 1 :(得分:0)

尝试此控件:https://github.com/jonasman/JNExpandableTableView 它支持你说的话。点击一个单元格可以扩展它。