如何在需要单击一个单元格时显示表格视图单元格

时间:2010-12-21 11:57:35

标签: iphone objective-c ios ipad

我正在该应用程序中编写一个应用程序我必须显示带有两个单元格的tableview。当用户触摸的第一个单元格然后它必须在视图中显示一些其他单元格(我不能在这里使用导航)以及第二个单元格也可见..任何人都可以告诉我这个建议。 谢谢大家。

3 个答案:

答案 0 :(得分:1)

如果要在单击单元格时插入表格视图单元格,请尝试以下代码。

让我们考虑您要在0区的第1,2和3位插入​​3行。在 didSelectRowAtIndexPath 方法中,执行以下操作。如果我们在第0部分(第一部分)中选择第0行(第一行),那么这些行将插入第0部分。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

   if (indexPath.row == 0 && indexPath.section == 0) {

       // Create indexPaths for the rows you are going to insert

       NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:1 inSection:0]];
       NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:2 inSection:0]];
       NSIndexPath *indexPath3 = [NSIndexPath indexPathForRow:3 inSection:0]];

       // Increase the number of rows in section 0 by 3, as we are inserting 3 rows here

       numberOfRowsInSectionZero = numberOfRowsInSectionZero + 3;

       // Insert the rows

       [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath1, indexPath2, indexPath3, nil] withRowAnimation:UITableViewRowAnimationFade];
    }
}

numberOfRowsInSection 方法中,您应该拥有以下内容,

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

    // some code here

    if (section == 0) return numberOfRowsInSectionZero;

    // some code here
}

确保您在 cellForRowAtIndexPath 方法中使用适当的代码,以便在新显示的行中显示内容。

注意:您必须更改代码,以便在单击0部分的第0行时插入时不插入行。

答案 1 :(得分:0)

制作一个bool(isRowClick)变量和

在didSelectRow中设置YES,然后写入此行

[yourTable reloadData];

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

if(isRowClick)
  return 7;//according to you  
else
  return 2;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

  isRowSelect=YES;
 //stuff
}

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

//stuff according to you
}

这会扼杀你。

答案 2 :(得分:0)

  1. 定义数据对象,例如的NSMutableDictionary
  2. 始终读取此对象的数据(对于计数以及您要显示的值)
  3. 在用户单击已定义的单元格的情况下。你将刷新MutableDirectory并添加新数据。
  4. 重新加载TableViewController [tableviewController.tableview reloadData];
  5. 就是这样 欢呼西蒙