iphone开发:如何在UITableView上放置UIView

时间:2013-02-11 15:19:02

标签: iphone ios objective-c uitableview uiview

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

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

5 个答案:

答案 0 :(得分:7)

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

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

答案 1 :(得分:1)

使用tableHeaderView属性。

返回表格上方显示的辅助视图。

@property(nonatomic, retain) UIView *tableHeaderView

表标题视图与节标题不同。

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

答案 2 :(得分:0)

让我们假设您的视图隐藏/显示在表视图的顶部是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;
    }

}
希望它有所帮助。

答案 3 :(得分:0)

你也可以在前面看到。

[view bringsubviewtofront];

答案 4 :(得分:0)

我有一个类似的问题,我想在我的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]; 
        }