【问题标题】:Add buttons under a tableview在 tableview 下添加按钮
【发布时间】:2009-07-24 15:40:10
【问题描述】:

我正在尝试以编程方式创建视图。我想要的结果是一个滚动视图,里面有一个表格视图。在这个表格视图下我想添加一些按钮

我不知道该怎么做,我试过了,但它不起作用:

- (void)loadView {
    [super loadView];

    tableView = [[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped];
    [tableView setDelegate:self];
    [tableView setDataSource:self];

    scrollView = [[UIScrollView alloc] initWithFrame:[[self view] bounds]];
    //[scrollView setBackgroundColor:[UIColor blackColor]];
    [scrollView setBouncesZoom:YES];

    deconnectButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    [deconnectButton setTitle:@"Deconect" forState:UIControlStateNormal];
    [deconnectButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];

    //[deconnectButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; 
    deconnectButton.frame = tableView.frame;
    NSLog(@"Tableview frame : %@", NSStringFromCGRect(tableView.frame));

    [scrollView addSubview:deconnectButton];

    [scrollView addSubview:tableView];


    [[self view] addSubview:scrollView];


}

我错过了什么或做错了什么?

【问题讨论】:

    标签: objective-c uitableview uiview uiscrollview


    【解决方案1】:

    其实我找到了解决办法。 tableview 有一个名为 tableFooterView 的属性。您所要做的就是:

    -创建一个 UIView - 向此视图添加一个按钮 -最后设置在tableFooterView上

    代码如下:

    tableView = [[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped];
    [tableView setDelegate:self];
    [tableView setDataSource:self];
    
    // create a UIButton (Deconnect button)
    UIButton *btnDeco = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnDeco.frame = CGRectMake(0, 0, 280, 40);
    [btnDeco setTitle:@"Déconnecter" forState:UIControlStateNormal];
    btnDeco.backgroundColor = [UIColor clearColor];
    [btnDeco setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [btnDeco addTarget:self action:@selector(deconnect:) forControlEvents:UIControlEventTouchUpInside];
    
    // create a UIButton (Change pseudo button)
    UIButton *btnChange = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnChange.frame = CGRectMake(0, 50, 280, 40);
    [btnChange setTitle:@"Changer Pseudo" forState:UIControlStateNormal];
    btnChange.backgroundColor = [UIColor clearColor];
    [btnChange setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [btnChange addTarget:self action:@selector(changePseudo:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //create a footer view on the bottom of the tabeview
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(20, 0, 280, 100)];
    [footerView addSubview:btnDeco];
    [footerView addSubview:btnChange];
    
    tableView.tableFooterView = footerView; 
    [footerView release];
    
    [[self view] addSubview:tableView];
    

    【讨论】:

      【解决方案2】:

      需要注意的一点是,UITableView 是 UIScrollView 的子类,因此您可能必须以不同于仅让它进行滚动的方式管理 UITableView 的大小。

      您的代码似乎将 tableView 和 deconnectButton 设置为相同的大小,并且该大小是 scrollView 超级视图的大小。我希望这会影响 tableView 遮挡按钮。

      根据您的描述,听起来您应该根据其内容计算表格的大小,然后相应地设置其框架。然后将按钮的框架设置在其下方。此外,您需要使用其 contentSize 属性设置滚动视图的大小。这种情况下的问题是您必须始终保持滚动视图的大小和按钮的位置与 tableView 的大小同步。

      您可能会调查将表格中的最后一行设为按钮并消除外部滚动视图。最后可能会导致代码更少。

      【讨论】:

        【解决方案3】:

        如果您在 UINavigationController 中有 UITableView,您可以在 UITableViewController / UIViewController 的底部设置工具栏项。

        UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];
        self.toolbarItems = @[barButton];
        

        请记得像这样显示工具栏:

        self.navigationController.toolbarHidden = NO;
        
        //or animated
        [self.navigationController setToolbarHidden:NO animated:YES];
        

        这可能比在你的桌子下面破解视图更干净。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-12-19
          • 1970-01-01
          • 2013-05-10
          • 2015-01-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多