2013-05-14 117 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; 


} 
相關問題