我有一个应用程序,其中UITableView的分隔符插入设置为自定义值-右0,左0。这在iOS 7中非常有效。x,但是在iOS 8.0中,我看到右边的分隔符被设置为默认值15。即使在xib文件中它设置为0,它仍然不正确地显示。
如何删除UITableViewCell分隔符边距?
我有一个应用程序,其中UITableView的分隔符插入设置为自定义值-右0,左0。这在iOS 7中非常有效。x,但是在iOS 8.0中,我看到右边的分隔符被设置为默认值15。即使在xib文件中它设置为0,它仍然不正确地显示。
如何删除UITableViewCell分隔符边距?
当前回答
使用下面的代码片段避免不必要的填充问题UITableView在IOS 8和7。
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView respondsToSelector:@selector(setSeparatorInset:)])
{
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:@selector(setLayoutMargins:)])
{
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)])
{
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
其他回答
对我来说,简单的线条就够了
cell.layoutMargins = UIEdgeInsetsZero
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ... Get the cell
cell.separatorInset = UIEdgeInsetsMake(0.f, 20.f, 0.f, [UIScreen mainScreen].bounds.size.width - 20);
// others
return cell;
}
对于要隐藏分隔符的任何特定单元格。
在iOS8:
添加这个到我的UITableViewCell子类:
- (UIEdgeInsets)layoutMargins {
return UIEdgeInsetsZero;
}
和这个到"tableView:cellForRowAtIndexPath"或"tableView:willDisplayCell":
[editCell setSeparatorInset:UIEdgeInsetsZero];
对我有用。
iOS在单元格和表视图上引入了layoutmargin属性。
这个属性在iOS 7.0中不可用,所以你需要确保在分配它之前检查!
然而,苹果已经添加了一个名为preservessuperviewlayoutmargin的属性到你的单元格,这将阻止它继承你的表视图的边距设置。这样,单元格可以独立于表视图配置它们自己的边距。把它看作是一个重写。
这个属性叫做preservessuperviewlayoutmargin,将它设置为NO可以让你用你自己单元格的layoutMargin设置来覆盖你的表格视图的layoutMargin设置。它既节省时间(你不必修改表视图的设置),也更简洁。详情请参考Mike Abdullah的回答。
注意:这是正确的,较少混乱的实现,如Mike Abdullah的回答所示;设置你的单元格的preservessuperviewlayoutmargin =NO将确保你的表格视图不会覆盖单元格设置。
第一步-设置单元格边距:
/*
Tells the delegate that the table view is about to draw a cell for a particular row.
*/
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath)
{
// Remove separator inset
if cell.respondsToSelector("setSeparatorInset:") {
cell.separatorInset = UIEdgeInsetsZero
}
// Prevent the cell from inheriting the Table View's margin settings
if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
cell.preservesSuperviewLayoutMargins = false
}
// Explictly set your cell's layout margins
if cell.respondsToSelector("setLayoutMargins:") {
cell.layoutMargins = UIEdgeInsetsZero
}
}
将单元格上的preservessuperviewlayoutmargin属性设置为NO可以防止表视图覆盖单元格边缘。在某些情况下,它似乎不能正常工作。
第二步——只有在所有失败的情况下,你才可以强制使用你的表视图边距:
/*
Called to notify the view controller that its view has just laid out its subviews.
*/
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// Force your tableview margins (this may be a bad idea)
if self.tableView.respondsToSelector("setSeparatorInset:") {
self.tableView.separatorInset = UIEdgeInsetsZero
}
if self.tableView.respondsToSelector("setLayoutMargins:") {
self.tableView.layoutMargins = UIEdgeInsetsZero
}
}
...好了!这应该可以在iOS 8和iOS 7上运行。
注意:使用iOS 8.1和7.1进行测试,在我的情况下,我只需要使用本解释的第一步。
第二步只需要在渲染的单元格下面有未填充的单元格。如果表的行数大于表模型中的行数。不执行第二步将导致分隔符偏移量不同。
只需重写cellForRowAtIndexPath并设置cell。preservessuperviewlayoutmargin = false" and "cell。separatorInset = UIEdgeInsets。零”和“单元格”。layoutmargin = uiedgeinsets . 0 "
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: LayoutTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "LayoutCell") as? LayoutTableViewCell
let currentLayout = OrderLayouts()[indexPath.row]
cell.NameLabel?.text = currentLayout.name
cell.DescrLabel?.text = currentLayout.descr
if(GlobalVariables.debug){
cell.DescrLabel?.text = "\(currentLayout.id) \n \(currentLayout.descr)"
}
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
return cell
}