首页 文章

我可以强制UITableView隐藏空单元格之间的分隔符吗? [重复]

提问于
浏览
197

这个问题在这里已有答案:

当使用具有足够大的单元格的普通样式 UITableViewUITableView 无法在不滚动的情况下显示它们,单元格下方的空白区域中不会出现分隔符 . 如果我只有几个单元格,则它们下面的空白区域包含分隔符 .

有没有办法可以强制 UITableView 删除空白区域中的分隔符?如果不是,我将不得不加载自定义背景,并为每个单元格绘制一个分隔符,这将使其更难继承行为 .

我发现了一个类似的问题here,但我不能在我的实现中使用分组 UITableView .

10 回答

  • 114

    您可以通过为tableview定义页脚来实现所需的功能 . 有关详细信息,请参阅此答案:Eliminate Extra separators below UITableView

  • 8

    适用于iOS 7. *和iOS 6.1

    最简单的方法是设置 tableFooterView 属性:

    - (void)viewDidLoad 
    {
        [super viewDidLoad];
    
        // This will remove extra separators from tableview
        self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    }
    

    对于以前的版本

    你可以将它添加到你的TableViewController(这将适用于任意数量的部分):

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
         // This will create a "invisible" footer
         return 0.01f;
     }
    

    if it is not enough ,添加以下代码 too

    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
    {        
        return [UIView new];
    
        // If you are not using ARC:
        // return [[UIView new] autorelease];
    }
    
  • 22

    对于Swift:

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView()  // it's just 1 line, awesome!
    }
    
  • 0

    使用Daniel的链接,我做了一个扩展,使它更有用:

    //UITableViewController+Ext.m
    - (void)hideEmptySeparators
    {
        UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
        v.backgroundColor = [UIColor clearColor];
        [self.tableView setTableFooterView:v];
        [v release];
    }
    

    经过一些测试,我发现大小可以是0,它也可以 . 所以它不会在表的末尾添加某种边距 . 所以,谢谢wkw这个黑客 . 我决定在这里发帖,因为我不喜欢重定向 .

  • 67

    Swift 版本

    最简单的方法是设置tableFooterView属性:

    override func viewDidLoad() {
        super.viewDidLoad()
        // This will remove extra separators from tableview
        self.tableView.tableFooterView = UIView(frame: CGRectZero)
    }
    
  • 5

    对于Swift:

    self.tableView.tableFooterView = UIView(frame: CGRectZero)
    
  • 213

    如果您使用iOS 7 SDK,这非常简单 .

    只需在viewDidLoad方法中添加以下行:

    self.yourTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
  • 10

    将表的 separatorStyle 设置为 UITableViewCellSeparatorStyleNone (在代码中或在IB中)应该可以解决问题 .

  • 105

    我使用以下内容:

    UIView *view = [[UIView alloc] init];
    myTableView.tableFooterView = view;
    [view release];
    

    在viewDidLoad中执行此操作 . 但你可以在任何地方设置它 .

  • 7

    以下这个问题对我来说非常有用:

    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    CGRect frame = [self.view frame];
    frame.size.height =  frame.size.height - (kTableRowHeight * numberOfRowsInTable);
    
    UIView *footerView = [[UIView alloc] initWithFrame:frame];
    return footerView; }
    

    其中kTableRowHeight是我的行单元格的高度,numberOfRowsInTable是我在表格中的行数 .

    希望有所帮助,

    布伦顿 .

相关问题