我在一个viewcontroller中有tableview.我有一节.我想在页脚中添加按钮.我写了这段代码,但页脚视图没有显示.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)]; UIButton *addcharity=[UIButton buttonWithType:UIButtonTypeCustom]; [addcharity setTitle:@"Add to other" forState:UIControlStateNormal]; [addcharity addTarget:self action:@selector(addCharity:) forControlEvents:UIControlEventTouchUpInside]; addcharity.frame=CGRectMake(0, 0, 40, 30); [footerView addSubview:addcharity]; return footerView; }
我还在其委托方法中设置了页脚100的高度.
截图:最初我有3个数据.但是当我搜索特定数据并且结果将在tableview中显示时,我有一个数据,所以那时页脚位置发生了变化.我想在最初的地方修剪页脚.
编辑:
作为替代解决方案,可以通过在末尾添加额外的单元来添加按钮.
根据Apple的Docs,您还必须实施该heightForFooterInSection
方法,否则您viewForFooterInSection
不会做任何事情.
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 100.0f; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { if(tableView == myListTableview) //Here you can make decision { UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)]; UIButton *addcharity=[UIButton buttonWithType:UIButtonTypeCustom]; [addcharity setTitle:@"Add to other" forState:UIControlStateNormal]; [addcharity addTarget:self action:@selector(addCharity:) forControlEvents:UIControlEventTouchUpInside]; [addcharity setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; //Set the color this is may be different for iOS 7 addcharity.frame=CGRectMake(0, 0, 130, 30); //Set some large width for your title [footerView addSubview:addcharity]; return footerView; } } - (void)addCharity:(id)sender { NSLog(@"add to charity"); }