【问题标题】:UITableView - inserting rows and scrolling to a specific sectionUITableView - 插入行并滚动到特定部分
【发布时间】:2012-08-06 20:40:45
【问题描述】:

我有一个类似的表格视图..

单元格 xxxx、yyyy 和 zzzz 是固定的,因此不会对它们进行点击操作。但是单元格“显示”是可点击的。单击“显示”单元格时,我想在“显示”单元格下显示一些六个单元格。

因此,单击“显示”单元格后,表格将如下所示..

在这里,我所做的是,

  • cell.textLabel.text 更改为“显示”为“隐藏”

  • tableView:didSelectRowAtIndexPath: 方法中使用了[tableView reloadData];

我的代码是:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"My title";
    // This Mutable array contains the hided cell objects
    hidedCellsArray = [[NSMutableArray alloc] initWithObjects:@"Cell 1", @"Cell 2", @"Cell 3", @"Cell 4", @"Cell 5", @"Cell 6", nil];
}


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

else if(indexPath.section == 1)
    {
        if (isShowClicked) //isShowClicked is a boolean value that determines whether the show cell was clicked or not
        {
            if (indexPath.row == 0)
            {
                cell.textLabel.text = @"Hide";
                cell.selectionStyle = UITableViewCellSelectionStyleBlue;
                cell.textLabel.textAlignment=UITextAlignmentCenter;
            }

            else
            {
                cell.textLabel.text = [hidedCellsArray objectAtIndex:indexPath.row-1];
                cell.selectionStyle = UITableViewCellSelectionStyleBlue;
                cell.textLabel.textAlignment=UITextAlignmentCenter;
            }
        }

        else
        {
            cell.textLabel.text = @"Show";
            cell.selectionStyle = UITableViewCellSelectionStyleBlue;
            cell.textLabel.textAlignment=UITextAlignmentCenter;
        }
    }
...
return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (indexPath.section == 1)
    {
        if (isShowClicked)
        {
            isShowClicked = NO;
        }

        else
        {
            isShowClicked = YES;
        }

        [tableView reloadData];
    }
}

我需要什么:

  • 我想在单击显示/隐藏按钮时为单元格设置动画。我了解到,应该使用insertRowsAtIndexPaths: withRowAnimation:的方法来实现插入效果。但我真的没有看到任何简单的例子。我是否应该包含任何其他方法,例如 setEditing:animated:tableView: commitEditingStyle: forRowAtIndexPath: 方法来执行此操作?

  • 第二件事是,在动画(单元格插入)发生之前,我想将 tableview 移动到第 2 节(即“显示”单元格)到顶部。然后应该发生插入单元格的动画。因此,单击显示单元格后表格的最终外观应该是......

需要帮助..我只是困惑!

【问题讨论】:

    标签: objective-c uitableview animation scroll insertion


    【解决方案1】:

    对于第一部分,您可以查看Table View Programming Guide for iOS 下的“行和节的批量插入、删除和重新加载”部分。最重要的是您需要确保您的数据源与更改相匹配。

    对于第二个问题,您可以将表格视图的contentOffset 设置为第二节标题的原点。比如:

    self.tableView.contentOffset = CGPointMake(0, [self.tableView rectForSection:1].origin.y);
    

    如果你想使用动画来获得更好的 UE,就这样做

    [UIView animateWithDuration:duration animations:^{
        //Move table view to where you want
    } completion:^(BOOL finished){
        //insert rows when table view movement animation finish
    }];
    

    【讨论】:

      【解决方案2】:

      试试 UIView CommitAnimation

      【讨论】:

        【解决方案3】:

        不要吹毛求疵,但为了语法正确,“hidedArray”实际上应该是“hiddenArray”。 无论如何,我认为您不需要为隐藏项目设置单独的数组。举例来说,假设您正在使用“dataArray”来填充表格视图。

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
        {
           ...
        
             if ((indexpath.section == 1) && (indexpath.row == 0)) {
                  if (isShowClicked) {
                    cell.textLabel.text = @"Hide";
                    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
                    cell.textLabel.textAlignment=UITextAlignmentCenter;
                   } else {
                    cell.textLabel.text = @"Show";
                    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
                    cell.textLabel.textAlignment=UITextAlignmentCenter;
                   }
              }
        }
        
        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
        {
          if ((indexpath.section == 1) && (indexpath.row == 0) && (isShowClicked == NO)) {
            isShowClicked = YES;
            [self.tableView beginUpdates];
            [dataArray addObjectsFromArray:[NSArray arrayWithObjects:@"cell1",@"cell2",@"cell3",@"cell4",@"cell5",@"cell6", nil]];
            NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[dataArray count]-1 inSection:1]]; 
            //the first section is section 0, so this is actually the second one
            [[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
            [tableView endUpdates];
            showIsClicked = YES;
            self.tableview.contentOffset = xy; 
           //the value of the tableView's contentOffset when it is scrolled to the right position. You can get it by NSLog-ing the contentOffset property and scrolling to the desired position
           } else if ((indexpath.section == 1) && (indexpath.row == 0) && (isShowClicked == YES)) {
            isShowClicked = NO;
            [self.tableView beginUpdates];
                [array removeObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(indexpath.row + 1, 6)]];
        
            [tableView endUpdates];
            self.tableview.contentOffset = xy; 
           //the value of the tableView's contentOffset when it is scrolled to the right position. You can get it by NSLog-ing the contentOffset property and scrolling to the desired position
           }
        }
        

        可能有一些错误,但我认为大部分是正确的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-03-17
          • 1970-01-01
          • 1970-01-01
          • 2018-03-17
          • 2011-08-17
          • 2014-11-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多