Objective c UITableView-插入行并滚动到特定部分

Objective c UITableView-插入行并滚动到特定部分,objective-c,uitableview,animation,scroll,insertion,Objective C,Uitableview,Animation,Scroll,Insertion,我有一个像..这样的桌面视图 单元xxxx、yyyy和ZZZ是固定的,因此它们没有单击操作。但是单元格“显示”是可点击的。我想在单击“show”单元格时,在该单元格下显示大约六个单元格 因此,单击“Show”单元格后,表格将如下所示 在这里,我所做的是 将单元格.textlab.text改为“显示”改为“隐藏” 使用[tableView重载数据] 我的代码是: - (void)viewDidLoad { [super viewDidLoad]; self.title =

我有一个像..这样的桌面视图

单元xxxx、yyyy和ZZZ是固定的,因此它们没有单击操作。但是单元格“显示”是可点击的。我想在单击“show”单元格时,在该单元格下显示大约六个单元格

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

在这里,我所做的是

  • 单元格.textlab.text
    改为“显示”改为“隐藏”

  • 使用
    [tableView重载数据]tableView:didSelectRowAtIndexPath:
    方法中选择code>

我的代码是:

- (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:committeeditingstyle:forRowAtIndexPath:
    方法来执行此操作

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


需要帮助。。我只是糊涂了

尝试使用UIView CommitAnimation

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

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

self.tableView.contentOffset = CGPointMake(0, [self.tableView rectForSection:1].origin.y);
如果你想用动画来获得更好的效果,就这么做吧

[UIView animateWithDuration:duration animations:^{
    //Move table view to where you want
} completion:^(BOOL finished){
    //insert rows when table view movement animation finish
}];
不是吹毛求疵,但是为了语法正确,“hiddedaray”应该是“hiddedaray”。 无论如何,我不认为你需要一个单独的数组来隐藏项目。为了举例说明,假设您正在使用“dataArray”填充tableview

- (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
   }
}
可能会有一些错误,但我认为基本上是正确的