简体   繁体   English

如何在UITableView中的选定行中添加自定义视图?

[英]how to add a custom view in the selected row in UITableView?

I am new at programming and struggling in this. 我是编程的新手,并在这方面苦苦挣扎。 how to add a custom view in the selected row in UITableView? 如何在UITableView中的选定行中添加自定义视图?

All other rows should not be affected. 所有其他行不应受到影响。 The new view should appear in place of the row clicked. 新视图应出现在单击行的位置。 The view can have a bigger size. 视图可以有更大的尺寸。 Thanks. 谢谢。

You can add new view in rowDidSelect Delegate method, 您可以在rowDidSelect Delegate方法中添加新视图,

- (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];

}

Implement DidDeselectRowatindexpath also as given 同样实现DidDeselectRowatindexpath

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

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

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

[myView removeFromSuperview];
}
    - (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];

}

I havn't tried this but it may work let me know if it works. 我没有试过这个,但它可能会工作让我知道它是否有效。

- (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;


}

see this for add custom view in selected row of uitableview 看到这个用于在uitableview的选定行中添加自定义视图

How to add a content view on UITAbleViewCell when a row is selected? 如何在选择行时在UITAbleViewCell上添加内容视图?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM