如何显示表格视图单元格,当我们需要点击一个单元格时
问题描述:
我正在编写一个应用程序,我必须在两个单元格中显示tableview.while第一个单元格被用户触摸,然后它必须显示其他一些单元格在视图内(我不能使用导航在这里),也是第二个单元格也可见..任何人都可以告诉我这个建议。 谢谢大家。如何显示表格视图单元格,当我们需要点击一个单元格时
答
如果要插入表格视图单元,当你点击一个细胞,尝试下面的代码。
让我们考虑你要在第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时插入的行未插入。
答
使didSelectRow一个布尔(isRowClick)变量和
集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
}
这将HEP你。
答
- 定义数据对象,例如, NSMutableDictionary
- 总是读出这个对象的数据(对于count和你想要显示的值)
- 在用户点击定义的单元格的情况下。您将刷新MutableDirectory并添加新数据。
- 重新加载TableViewController
[tableviewController.tableview reloadData];
完蛋了 欢呼西蒙