2013-05-14 131 views
3

我是编程新手,在这方面挣扎。 如何在UITableView的选定行中添加自定义视图?如何在UITableView的选定行中添加自定义视图?

所有其他行不应受到影响。新视图应该出现在点击行的位置。视图可以有更大的尺寸。 谢谢。

+1

- (CGFloat的)的tableView:(UITableView的*)的tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 使用这种方法来改变使用上述代码,小区 – Bala 2013-05-14 11:25:40

回答

8

可以在rowDidSelect委托方法添加新的观点,

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

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 100.0f, 30.0f)]; 
[myView setTag:-1]; 

[cell addSubview:myView]; 

} 

也实现DidDeselectRowatindexpath给出

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

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

UIView *myView = [cell viewWithTag:-1]; 

[myView removeFromSuperview]; 
} 
+0

的大小当我点击一些其它行,子视图出现在先前点击的行上。这是为什么? – nitz19arg 2013-05-14 11:48:26

+0

请参阅编辑 – 2013-05-14 11:55:02

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

     NSLog(@"%ld",(long)indexPath.row); // 

     str=[appdelegate.name objectAtIndex:indexPath.row]; 

    DetailViewController *dvc = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];  //DetailViewController is your view 

     [self.navigationController pushViewController:dvc animated:YES]; 

} 
+0

嗨。谢谢。为什么你实例化* dvc两次? – nitz19arg 2013-05-14 11:33:33

+0

抱歉,这是我的错误,我忘了。 – Jitendra 2013-05-14 11:57:03

0

我没有试过这个,但它可能会让我知道它是否工作。

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

     UITableViewCell *cell = [tableview cellForRowAtIndexPath: indexpath]; 

     [cell.contentView addSubView: yourviewObject]; 

     cell.bounds.size.height = yourViewObject.bounds.size.height + 10.0; 


} 
相关问题