代码之家  ›  专栏  ›  技术社区  ›  jessecurry

是否可以强制UITableView隐藏空单元格之间的分隔符?[副本]

  •  201
  • jessecurry  · 技术社区  · 15 年前

    这个问题已经有了答案:

    使用普通样式时 UITableView 有足够多的细胞 UITababVIEW 无法在不滚动的情况下全部显示,单元格下方的空白区域中不显示分隔符。如果我只有几个单元格,它们下面的空白区域就包括分隔符。

    有什么方法可以强制 UITababVIEW 是否要删除空白区域中的分隔符?如果没有,我将不得不加载一个自定义背景,为每个单元格绘制一个分隔符,这将使继承行为更加困难。

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

    10 回复  |  直到 9 年前
        1
  •  115
  •   Community CDub    8 年前

    您可以通过为TableView定义一个页脚来实现您想要的。有关详细信息,请参阅此答案: Eliminate Extra separators below UITableView

        2
  •  213
  •   J. Costa    11 年前

    适用于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;
     }
    

    如果不够的话 ,添加以下代码 以下内容:

    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
    {        
        return [UIView new];
    
        // If you are not using ARC:
        // return [[UIView new] autorelease];
    }
    
        3
  •  112
  •   Sebastian    9 年前

    对于Swift:

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView()  // it's just 1 line, awesome!
    }
    
        4
  •  67
  •   Sauleil    14 年前

    使用Daniel的链接,我做了一个扩展以使其更可用:

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

    经过一些测试,我发现尺寸可以是0,而且也可以工作。所以它不会在表的末尾添加某种边距。所以感谢WKW的黑客。我决定把它贴在这里,因为我不喜欢重定向。

        5
  •  23
  •   King-Wizard    10 年前

    斯威夫特 版本

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

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

    Swift:

    self.tableView.tableFooterView = UIView(frame: CGRectZero)
    
        7
  •  8
  •   Salmo    11 年前

    如果您使用iOS7sdk,这非常简单。

    只需在viewDidLoad方法中添加此行:

    self.yourTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
        8
  •  7
  •   duncanwilcox    15 年前

    设置桌子的 separatorStyle UITableViewCellSeparatorStyleNone (在代码或ib中)应该做到这一点。

        9
  •  5
  •   Dare2Dream    13 年前

    我使用以下方法:

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

    在viewdidload中执行。但你可以把它放在任何地方。

        10
  •  0
  •   Callaghan001    13 年前

    在这个问题上,以下几点对我很有效:

    - (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是行单元格的高度,NumberOfRowInTable是表中的行数。

    希望能有所帮助,

    Brenton。