Ios 空时显示UITableView页脚
我有一个Ios 空时显示UITableView页脚,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个UITableView有时是空的,我有一个UITableView的页脚。我希望页脚始终显示,即使在表视图中没有内容。这可能吗?还是只需在UITableView中添加一个额外的单元格并将其用作页脚就更好了 希望将其作为页脚分开,而不是将其作为单元格添加到UITableView的末尾 非常感谢您的帮助。如果没有行,您可以使用类似的方法 UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
UITableView
有时是空的,我有一个UITableView
的页脚。我希望页脚始终显示,即使在表视图中没有内容。这可能吗?还是只需在UITableView
中添加一个额外的单元格并将其用作页脚就更好了
希望将其作为页脚分开,而不是将其作为单元格添加到UITableView
的末尾
非常感谢您的帮助。如果没有行,您可以使用类似的方法
UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
[footerView setBackgroundColor:[UIColor clearColor]];
self.tableView.tableFooterView = footerView;
[self.tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
[self.tableView setContentInset:(UIEdgeInsetsMake(0, 0, -350, 0))];
好的,第一件事是,正如您所说的“UITableView有时是空的”。因此,如果您的表是空的,并且您在其中添加了页脚视图。如下
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView* myFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];
[myFooterView setBackgroundColor:[UIColor blueColor]];
return myFooterView;
}
您的页脚视图将显示在tableview的顶部,因为tableview中并没有行。如下图所示。红色是表视图颜色,蓝色是页脚视图颜色
当您的表不是空的时候,页脚视图会是这样的
因此,我建议您在表视图下添加和额外视图,并在该视图中显示内容。希望这能消除您的疑虑。您只需调用具有HeightForFooterInstitution和ViewForFooterInstitution作为参数的表视图函数即可。在第一个示例中,返回页脚所需的高度,在第二个示例中,返回希望显示为页脚视图的视图。例如:
import UIKit
class ViewController: UIViewController {
@IBOutlet var tableView: UITableView!
var numberOfCells = 0
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(UINib(nibName: "ExampleCellTableViewCell", bundle: nil), forCellReuseIdentifier: "ExampleCellTableViewCell")
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numberOfCells
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = self.tableView.dequeueReusableCell(withIdentifier: "ExampleCellTableViewCell") as? ExampleCellTableViewCell else { return UITableViewCell() }
return cell
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 80.0
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return FooterView.instanceFromNib()
}
}
即使单元格数为0,页脚视图也会出现:
您可以在此处查看完整的示例项目:
对于任何想要有空/无文本的标准页脚(基本上只是一个空格)的人,您可以将空格“”设置为页脚文本。这样,页脚将显示出来,并且页脚高度将生效。我认为这太过分了,根本不喜欢编辑内容插页的想法。这不会产生任何问题。您找到解决方案了吗?