objective-c - 在 tableview 下添加按钮

标签 objective-c uitableview uiview uiscrollview

我正在尝试以编程方式创建 View 。我想要的结果是一个带有 tableview 的 ScrollView 。在这个表格 View 下我想添加一些按钮

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

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


}

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

最佳答案

实际上我找到了解决方案。 tableview 有一个名为 tableFooterView 的属性。您所要做的就是:

-创建一个UIView - 添加一个按钮到这个 View -最后设置在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];

关于objective-c - 在 tableview 下添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1178498/

相关文章:

iphone sdk - 调用 View 的 super View 的 View Controller 的方法

objective-c - UITabBar 重叠它的 View

ios - 为什么即使我在 NSDateFormatter 上设置了时区,stringFromDate NSString 仍然有 GMT 时间?

ios - 如何在选择行时通过更改 UIView 来制作 messageRead 指示器?

ios - Root View Controller 的 View 不在窗口层次结构中

ios - UITableView 异步图像加载快门通过多个图像

ios - 添加新项目后 UITableView 未更新(使用核心数据)

ios - 如何在 UIImageView/UIWebView 中转换 UIView

ios - 为什么在 UITableView 中自定义单元格会收到错误 : hasn't received initWithFrame: or initWithCoder?

Objective-C:Unicode 日期格式