我注意到在iOS 7中,UITableViewCells在iOS 6没有的单元格的分隔符中有换行符.有没有办法摆脱这个换行符?将分隔符更改为none,然后使用分隔符的颜色制作UIViews仍会导致白色分隔符无论如何都会发生.
Lui*_*ado 116
对于iOS7:
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
Run Code Online (Sandbox Code Playgroud)
对于iOS8:
首先配置表视图,如下所示:
if ([self.tableView respondsToSelector:@selector(layoutMargins)]) {
self.tableView.layoutMargins = UIEdgeInsetsZero;
}
Run Code Online (Sandbox Code Playgroud)
然后在您的cellForRowAtIndexPath:方法中,按如下方式配置单元格:
if ([cell respondsToSelector:@selector(layoutMargins)]) {
cell.layoutMargins = UIEdgeInsetsZero;
}
Run Code Online (Sandbox Code Playgroud)
注意:包括layoutMargins和separatorInset,以支持两个iOS版本
nul*_*ull 44
您还可以从故事板中设置" 分隔符插入 ":


小智 37
如果您想支持旧版本的iOS,则应在调用之前检查此方法的可用性:
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
Run Code Online (Sandbox Code Playgroud)
Ant*_*nko 16
弄清楚了.
[self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
Run Code Online (Sandbox Code Playgroud)
选定的答案对我不起作用.但这是肯定的,它适用于ios 2.0:
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
Run Code Online (Sandbox Code Playgroud)
iOS在单元格和表视图上引入了layoutMargins属性.
此属性在iOS 7.0中不可用,因此您需要确保在分配之前进行检查!
但是,Apple已向您的单元格添加了**属性,这将阻止它继承您的表格视图的边距设置.这样,您的单元格可以独立于表视图配置自己的边距.把它想象成一个覆盖.
此属性称为preservesSuperviewLayoutMargins,将其设置为NO可以允许您使用自己的单元格的layoutMargin设置覆盖Table View的layoutMargin设置.它既节省了时间(您不必修改表视图的设置),而且更简洁.有关详细说明,请参阅Mike Abdullah的答案.
注意:正如Mike Abdullah的回答所表达的那样,这是正确的,不那么混乱的实现; 设置单元格的preservesSuperviewLayoutMargins = NO将确保您的表视图不会覆盖单元格设置.
第一步 - 设置细胞边距:
/*
Tells the delegate 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 seperator 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
}
}
Run Code Online (Sandbox Code Playgroud)
将单元格上的preservesSuperviewLayoutMargins属性设置为NO 应该可以防止表视图覆盖单元格边距.在某些情况下,它似乎无法正常运作.
第二步 - 只有在所有操作都失败的情况下,您才可以强制执行Table View边距:
/*
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
}
}
Run Code Online (Sandbox Code Playgroud)
......你去!这应该适用于iOS 8和iOS 7.
注意:使用iOS 8.1和7.1测试,在我的情况下,我只需要使用此解释的第一步.
只有在渲染单元格下面有未填充的单元格时才需要第二步,即.如果表大于表模型中的行数.不执行第二步将导致不同的分隔符偏移.
对于应用程序范围内的永久解决方案,请在应用程序中调用它:didFinishLaunching ..任何tableview都将被"修复"
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
[[UITableView appearance]setSeparatorInset:UIEdgeInsetsZero];
}
Run Code Online (Sandbox Code Playgroud)
在iOS 8中,基于此处的Seth McFarlane帖子,您需要开始处理layoutMargins.以下为我做了:
在ApplicationDidFinishLaunching中,运行以下命令:
if ([UITableViewCell instancesRespondToSelector:@selector(setLayoutMargins:)]) {
[[UITableViewCell appearance]setLayoutMargins:UIEdgeInsetsZero];
}
Run Code Online (Sandbox Code Playgroud)在UITableView上创建以下类别:
@interface UITableView(WJAdditions)
-(void) wjZeroSeparatorInset;
@end
@implementation UITableView (WJAdditions)
-(void) wjZeroSeparatorInset {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
if ([self respondsToSelector:@selector(setLayoutMargins:)]) {
self.layoutMargins = UIEdgeInsetsZero;
}
#endif
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
if ([self respondsToSelector: @selector(setSeparatorInset:)])
self.separatorInset = UIEdgeInsetsZero;
}
#endif
}
@end
Run Code Online (Sandbox Code Playgroud)在UITableViews中,初始化后不久,调用
[self wjZeroSeparatorInset];
Run Code Online (Sandbox Code Playgroud)在UITableViewControllers中,您需要以下内容:
-(void) viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
UITableView* tv = self.tableView;
if ([tv respondsToSelector:@selector(setLayoutMargins:)]) {
[tv setLayoutMargins:UIEdgeInsetsZero];
}
}
Run Code Online (Sandbox Code Playgroud)在iOS 8中,似乎存在一个小错误.分隔符插入的自定义值不会应用于包含文本的单元格.我尝试从界面构建器和代码设置它,但都没有工作.我也提交了一份错误报告.

与此同时,我有一个小的解决方法来实现这一目标.我只是在细胞上绘制线条.创建一个子类UITableViewCell并覆盖该drawRect方法.另外不要忘记将separator的separator属性设置UITableView为None.这是代码.它在Swift中,但由于它基本上是C,你也可以在Objective-C中使用相同的东西.
override func drawRect(rect: CGRect) {
super.drawRect(rect)
let context = UIGraphicsGetCurrentContext()
CGContextSetStrokeColorWithColor(context, UITableView().separatorColor.CGColor) // seperator color
CGContextSetLineWidth(context, 2) // separator width
CGContextMoveToPoint(context, 0, self.bounds.size.height)
CGContextAddLineToPoint(context, self.bounds.size.width, self.bounds.size.height)
CGContextStrokePath(context)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36036 次 |
| 最近记录: |