0

私の UITableView には 1 つのセクションがあり、すべてのセルが表示されている場合は合計 6 になります。

表示および非表示にしたいセルは 1 つだけです。

概要は次のとおりです。

セル 0 - 常に表示

セル 1 - 常に表示

セル 2 - 常に表示

セル 3 - 常に表示

セル 4 - 最初は非表示 / セル 3 をタップすると表示または非表示になります

セル 5 - 常に表示

これは、didSelectRowAtIndexPath を使用してセルをアニメーション化/表示するために試したサンプルです。私が正しい軌道に乗っているかどうかはわかりませんが、誰かが見て、私がどこを台無しにしたかを理解するのを手伝ってくれたら、とても感謝しています!

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.row == 3)
    {
        if (self.indexPathSelected != indexPath)
        {
            [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.indexPathSelected.row+1 inSection:self.indexPathSelected.section]].hidden = YES;

            [tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];

            [tableView beginUpdates];

            [[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section]] performSelector:@selector(setHidden:) withObject:NO afterDelay:0.25];

            self.indexPathSelected = indexPath;

            [tableView endUpdates];

            return;
        }
    }
}

したがって、セル 3 をタップすると、最後のセルがちらつくだけです。

4

1 に答える 1

0

考慮すると、テーブルビューには合計6行があり、セクションは1つだけです。これにより、変更可能な配列を作成してテーブルビューのすべてのオブジェクトを保持し、セレクターを使用するinsertRowsAtIndexPaths: withRowAnimation:代わりdeleteRowsAtIndexPaths: withRowAnimation:に次のコードを使用して、非表示と表示の方法の例を示しますテーブルビュー セル


 mutArray = [[NSMutableArray alloc]initWithObjects:@"cell 1",@"cell 2",@"cell 3",@"cell 4",@"cell 6", nil]; //initially ur array contains only 5 objects notice that "cell 5" is not  there this will be hidden

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

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {

     return [mutArray count];
  }

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

   UITableViewCell *cell = [self.aTableView dequeueReusableCellWithIdentifier:@"cell"];
   if(mutArray.count != 6) //initially only 5 rows are present
   {
      if(cell == nil)
       {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

       }

    cell.textLabel.text = [mutArray objectAtIndex:indexPath.row];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    return cell;
 }
  else //for 5th row it must be a custom cell
   {
     return [mutArray objectAtIndex:indexPath.row];//becz u are already initilised ur custom cell just return it
  }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row == 3)
  {
      int totalRows = [tableView numberOfRowsInSection:indexPath.section];

      [tableView beginUpdates];
      if(totalRows == 5)
       {
        //initilise ur custom cell and add it to datasource array
        CustomCell *customCell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"customCell"];
        customCell.textLabel.text = @"cell 5";
        [mutArray insertObject:customCell atIndex:4];
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForItem:indexPath.row + 1 inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationFade];

      }
    else
      {
        [mutArray removeObjectAtIndex:5];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForItem:indexPath.row + 1 inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationFade];
     }
    [tableView endUpdates];
 }
}


すべての ur セルがカスタムの場合、-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathメソッドに来る必要はありません

これが役立つことを願っています:)

于 2013-09-23T06:51:38.790 に答える