当我设置一个有4行的表格视图时,在填充行的下面仍然有额外的分隔行(或额外的空白单元格)。

如何移除这些单元格?


当前回答

试试这个

self.tables.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 10.0f)];

其他回答

只需添加一个视图,以所需的分隔符颜色为背景色,宽度为100%,高度为1px,位置为x0 y-1到你的tableViewCell。确保tableViewCell不剪辑子视图,而应该是tableView。

所以你得到了一个绝对简单的和工作的分离只在现有的单元之间没有任何hack每个代码或IB。

注意:在垂直顶部反弹时,第一个分隔符会出现,但这应该不是问题,因为这是iOS的默认行为。

我想扩展一下我的回答:

简单地添加高度为0的页脚就可以了。(在SDK 4.2, 4.4.1上测试)

- (void) addFooter
{
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];

    [self.myTableView setTableFooterView:v];
}

或者更简单——在你设置tableview的地方,添加这一行:

//change height value if extra space is needed at the bottom.
[_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectMake(0,0,0,0)]];

或者更简单——简单地删除任何分隔符:

[_tableView setTableFooterView:[UIView new]];

再次感谢wkw:)

如果你在你的视图中有一个搜索栏(例如限制结果的数量),你还必须在shouldReloadTableForSearchString和shouldReloadTableForSearchScope中添加以下内容:

controller.searchResultsTable.footerView = [ [ UIView alloc ] initWithFrame:CGRectZero ];

在Swift中(我使用4.0),你可以通过创建一个自定义UITableViewCell类来实现这一点,并覆盖setSelected方法。然后分隔符将全部插入到0。(我的主类与表视图有一个清晰的背景)颜色。

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // eliminate extra separators below UITableView
    self.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}

界面构建器(iOS 9+)

只需将一个UIView拖到表中。在故事板中,它将位于自定义单元格下方的顶部。您可能更喜欢将其命名为“footer”。

为了清晰起见,这里显示为绿色,你可能想要清晰的颜色。

请注意,通过调整高度,您可以根据自己的喜好影响如何处理表的“底部反弹”。(高度通常为0)。


用程序来做:

斯威夫特

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.tableFooterView = UIView()
}

objective - c

iOS 6 . 1 +

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // This will remove extra separators from tableview
    self.tableView.tableFooterView = [UIView new];
}

如果你愿意,

    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

从iOS来看:

添加到表视图控制器…

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

如果有必要……

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{        
    return [UIView new];

    // If you are not using ARC:
    // return [[UIView new] autorelease];
}