【问题标题】:iphone development: how to put a UIView over UITableView [duplicate]iphone开发:如何在UITableView上放置一个UIView [重复]
【发布时间】:2013-02-11 15:19:01
【问题描述】:

在我的应用程序中,我使用情节提要。我在故事板中的元素之一是 UITableViewController。所以它里面有一个tableview。

我的问题是如何在这个 tableview 上放置一个 UIView。它将被隐藏,我想在按下 tableview 中的 tableviewcell 时使其可见。那可能吗?我该怎么做?

【问题讨论】:

  • 我不明白最后一部分 - 视图会隐藏表格,但用户会选择一个单元格来关闭它?

标签: iphone ios objective-c uitableview uiview


【解决方案1】:

最好的解决方案是在 StoryBoard 中使用普通视图控制器 (UIViewController)。您的控制器需要实现两个协议(UITableViewDataSource 和 UITableViewDelegate),并且您需要在视图控制器的视图中添加 UITablewView。

在这种情况下,在界面构建器中,您将能够将任何视图放在视图控制器的视图中(可以将其放在表视图上方)。

【讨论】:

  • 我也认为这是最好的方法,因为我也可以在故事板中看到视图。否则,我必须通过为 UIView 编写代码来完成所有工作。谢谢
【解决方案2】:

使用 tableHeaderView 属性。

返回显示在表格上方的附件视图。

@property(nonatomic, retain) UIView *tableHeaderView

表头视图不同于节头。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html

【讨论】:

    【解决方案3】:

    让我们假设您的视图被隐藏/显示在表视图的顶部是 topView,声明为全局变量。 将 .h 中的 topView 声明为

     UIView *topView;
    

    现在假设你有 UITableViewController 对象作为 tableViewController 然后,在 tableViewController 类的 viewDidLoad 中初始化 topView

    -(void)viewDidLoad
    {
             topView=[[UIView alloc] initWithFrame:yourNeededFrameSize];
             [self.tableView addSubview:topView];//tableView is a property for UITableViewController inherited class 
             topView.hidden=YES;//Hide it initially for the first time. 
    }
    

    假设您在此处实现了 UITableViewDelegate 方法,您将在 didSelectRowAtIndexPath 中执行此操作

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if(topView.isHidden==YES)
        {
            topView.hidden=NO;
        }
        else
        {
            topView.hidden=NO;
        }
    
    }
    

    希望对您有所帮助。

    【讨论】:

    • 在您的示例中,topView 将是 ivar 而不是全局变量。你也忘了打[super viewDidLoad];
    【解决方案4】:

    你也可以看到前面。

    [view bringsubviewtofront];
    

    【讨论】:

      【解决方案5】:

      我有一个类似的问题,我想在我的 UITableViewController 上添加一个加载指示器。为了解决这个问题,我将 UIView 添加为窗口的子视图。这解决了问题。我就是这样做的。

      -(void)viewDidLoad{
                  [super viewDidLoad];
                  //get the app delegate
                  XYAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
      
                  //define the position of the rect based on the screen bounds
                  CGRect loadingViewRect = CGRectMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2, 50, 50);
                  //create the custom view. The custom view is a property of the VIewController
                  self.loadingView = [[XYLoadingView alloc] initWithFrame:loadingViewRect];
                  //use the delegate's window object to add the custom view on top of the view controller
                  [delegate.window addSubview: loadingView]; 
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-25
        • 2011-01-16
        • 1970-01-01
        • 2012-08-07
        • 2012-08-26
        • 1970-01-01
        相关资源
        最近更新 更多