簡體   English   中英

那時我運行應用程序時,將在表格視圖中展開所有單元格

[英]When I run app that time I will expand all cell in tableview

我是iOS開發人員的新手。 當我運行該應用程序比時間我

在此處輸入圖片說明

但我會得到這種類型的輸出。

在此處輸入圖片說明

這是我的代碼

- (void) iterateItems:(NSArray*) array
{
    level++;

    for (int i = 0; i < [array count]; i++)
    {
        commentInfo = [array objectAtIndex:i];
        //NSLog(@"data=%@",commentInfo);
        Item *item = [[Item alloc] init];
        item.Id = [[[commentInfo objectForKey:@"sub_items"]objectAtIndex:0]objectForKey:@"name"];


        item.parentId = [[[commentInfo objectForKey:@"sub_items"]objectAtIndex:0]objectForKey:@"name"];

        if (item.level == 0)
        {
         item.title =[commentInfo objectForKey:@"dates"];
         item.title1 =[commentInfo objectForKey:@"id"];

        }
        else
        {
            item.title=@"abc";
        }

        //NSLog(@"title is=%@",[commentInfo objectForKey:@"status"]);
        item.level = level;
        item.visibility = @"normal";
        item.childVisibility = @"normal";
        [items addObject:item];

        [self iterateItems:[commentInfo objectForKey:@"sub_items"]];

    }

    level--;
}

- (float) cellHeightForRow:(int) row
{
    Item *item = [items objectAtIndex:row];

    if ([item.visibility isEqualToString:@"hidden"])
    {
        return 0.0f;

    }
    else
    {
        return 50.0f;
    }
}

- (void) hideCompleteNode:(UISwipeGestureRecognizer*) gestureRecognizer
{
    UITableViewCell *cell = (UITableViewCell*) [[gestureRecognizer view] superview];
    NSIndexPath *indexPath = [self.tblView indexPathForCell:cell];

    Item *item = [items objectAtIndex:indexPath.row];

    if (item.level == 0)
    {
        if ([item.childVisibility isEqualToString:@"normal"])
        {
            [self hideChilds:indexPath];
        }
    }
    else
    {
        int prevIndex = indexPath.row-1;

        if (prevIndex >= 0)
        {
            Item *parentItem = [items objectAtIndex:prevIndex];

            while (parentItem.level != 0)
            {
                prevIndex--;

                if (prevIndex >= 0)
                    parentItem = [items objectAtIndex:prevIndex];
                else
                    break;
            }
        }

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:prevIndex inSection:0];

        [self hideChilds:indexPath];
    }
}

- (void) hideChilds:(NSIndexPath*) indexPath
{
    NSMutableArray *indexPaths = [[NSMutableArray alloc] initWithObjects:indexPath, nil];

    Item *item = [items objectAtIndex:indexPath.row];

    BOOL shouldHide = [item.childVisibility isEqualToString:@"hidden"]?NO:YES;

    item.childVisibility = shouldHide?@"hidden":@"normal";

    int nextIndex = indexPath.row+1;

    if (nextIndex <  [items count])
    {
        Item *childItem = [items objectAtIndex:nextIndex];

        while (childItem.level > item.level)
        {
            childItem.visibility = shouldHide?@"hidden":@"normal";
            childItem.childVisibility = shouldHide?@"hidden":@"normal";

            [indexPaths addObject:[NSIndexPath indexPathForRow:nextIndex inSection:0]];

            nextIndex++;

            if (nextIndex < [items count])
                childItem = [items objectAtIndex:nextIndex];
            else
                break;
        }
    }

    [self.tblView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}

#pragma mark -
#pragma mark - UITableView Datasource and Delegate

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [items count];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self cellHeightForRow:indexPath.row];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellIdentifier = @"itemCell";

    UITableViewCell *cell = [self.tblView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        UIImageView *arrowImgView = [[UIImageView alloc] init];
        [arrowImgView setBackgroundColor:[UIColor clearColor]];
        [arrowImgView setTag:1];
        [cell.contentView addSubview:arrowImgView];
                     UILabel *titleLbl = [[UILabel alloc] init];
        [titleLbl setFont:[UIFont fontWithName:@"HelveticaNeue" size:13.0f]];
        [titleLbl setTextColor:[UIColor blackColor]];
        titleLbl.textAlignment = NSTextAlignmentCenter;


        [titleLbl setTag:2];
        [titleLbl setBackgroundColor:[UIColor clearColor]];
        [cell.contentView addSubview:titleLbl];

        UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hideCompleteNode:)];
        [swipeGesture setDirection:UISwipeGestureRecognizerDirectionLeft];
        [cell.contentView addGestureRecognizer:swipeGesture];
    }

    Item *item = [items objectAtIndex:indexPath.row];

    if (item.level == 0)
    {
        [cell.contentView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_background.png"]]];
    }
    else
    {

        [cell.contentView setBackgroundColor:[UIColor clearColor]];
    }

    UIImageView *arrowImgView = (UIImageView*) [cell viewWithTag:1];
    UILabel *titleLbl = (UILabel*) [cell viewWithTag:2];

    if ([item.visibility isEqualToString:@"hidden"])
    {
        [titleLbl setFrame:CGRectZero];
        [arrowImgView setFrame:CGRectZero];
    }
    else
    {
        [arrowImgView setFrame:CGRectMake(5.0f+(20.0f*item.level), 20.0f, 10.0f, 10.0f)];

        if ([item.childVisibility isEqualToString:@"hidden"])
        {
            [arrowImgView setImage:[UIImage imageNamed:@"closeArrow"]];
        }
        else
        {
            [arrowImgView setImage:[UIImage imageNamed:@"openArrow"]];
        }

        [titleLbl setFrame:CGRectMake(20.0f+(20.0f*item.level), 10.0f, 320.0f - (20.0f+(20.0f*item.level)), 30.0f)];
        [titleLbl setText:item.title];
    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.layer.shadowOpacity = 0.3;
    cell.layer.shadowColor = [UIColor blueColor].CGColor;
    cell.layer.shadowOffset = CGSizeMake(0.0, 4);

        [tblView setSeparatorColor:[UIColor redColor]];
        tblView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

    [tblView setSeparatorColor:[UIColor blackColor]];
    tblView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
   // [self hideChilds:indexPath];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell *)[self.tblView cellForRowAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor colorWithRed:0.004 green:0.482 blue:0.776 alpha:1];
    [self hideChilds:indexPath];
    Item *item = [items objectAtIndex:indexPath.row];

    if (item.level == 0)
    {

    }
    else
    {



        Item *item = [items objectAtIndex:indexPath.row];

        NSLog(@"value=%@",[NSString stringWithFormat:@"%@",item.title1]);

        approveleaseoffer *secondViewController =
        [self.storyboard instantiateViewControllerWithIdentifier:@"approveleaseofferpage"];
        secondViewController.offer_number=[NSString stringWithFormat:@"%@",item.title1];
        [self.navigationController pushViewController:secondViewController animated:YES];
}
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)viewDidLayoutSubviews
{
    if ([tblView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tblView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([tblView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tblView setLayoutMargins:UIEdgeInsetsZero];
    }
}

嘗試更改訂單項item.childVisibility = @"normal"; item.childVisibility = @"hidden"; iterateItems方法中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM