【发布时间】:2013-07-25 00:40:11
【问题描述】:
我使用-[UITableView setSeparatorColor:] 来设置附加图像中的红色边框。但是如何设置边框的颜色显示为白色?
编辑:我知道我可以使用UITableViewSeparatorStyleSingleLine 样式完全摆脱白色。但我不想那样做:我想改变它的颜色。谢谢!
【问题讨论】:
标签: ios objective-c cocoa-touch uitableview
我使用-[UITableView setSeparatorColor:] 来设置附加图像中的红色边框。但是如何设置边框的颜色显示为白色?
编辑:我知道我可以使用UITableViewSeparatorStyleSingleLine 样式完全摆脱白色。但我不想那样做:我想改变它的颜色。谢谢!
【问题讨论】:
标签: ios objective-c cocoa-touch uitableview
白色是因为分隔符样式设置为单线边缘。如果将其更改为单线,白线将消失。不确定这是否能解决您的问题,但我认为您不会在不做更多工作的情况下更改颜色。
【讨论】:
尝试为单元格制作“线视图”。因此,在 cellForRowAtIndexPath: 方法中,只需将其添加到您需要的单元格中:
UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.contentView.bounds.size.width, 1)] autorelease];
lineView.backgroundColor = [UIColor whiteColor];
lineView.autoresizingMask = 0x3f;
[cell.contentView addSubview:lineView];
这会将白线添加到每个单元格的顶部。如果您不想在第一行添加一个 if 语句:
if (indexPath.row != 0)
{
UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.contentView.bounds.size.width, 1)] autorelease];
lineView.backgroundColor = [UIColor whiteColor];
lineView.autoresizingMask = 0x3f;
[cell.contentView addSubview:lineView];
}
希望对你有帮助。
【讨论】:
您正在尝试更改默认 iOS 用户界面,但更改过于复杂,无法使用默认属性完成。
解决办法是什么?只需去掉 UITableVie 绘制的线条(设置颜色为[UIColor clearColor])并自定义 UITableViewCell 背景即可。
当然,您需要 3 种类型的背景 - 第一个单元格、最后一个单元格和中间的单元格:
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath (NSIndexPath*)indexPath {
UITableViewCell* cell = [...];
UIView* background;
if (indexPath.row == 0) {
background = [...]; //background for the first cell
}
else if (indexPath.row + 1 == [self tableView:tableView numberOfRowsInSection:indexPath.section]) {
background = [...]; //background for the last cell
}
else {
background = [...]; //background for the middle cells
}
cell.backgroundView = background;
}
问题是如何创建背景。通常我只使用一个 UIImageView 或几个 UIImageView 的组合(圆角作为一个图像,线条作为可调整大小的图像)。
【讨论】:
你不能。
此内置绘图特定于分组样式的 tableView,并且无法更改(据我所知...)
有一个解决方法(我们使用过 -> 抱歉没有源代码),但我猜你不会喜欢它:
1/ 拥有每个单元格的位置信息 2/ 重新实现每个单元格背景的绘制,取决于它的位置(所以你必须保存这个位置:FirstCell,MiddleCell,LastCell,) 来重现分组的 tableView 外观。
一种方法是使用CoreGraphics:
从您的子类 CellGroupedBackgroundView(或 customGroupedTableViewCell,取决于您选择的设计)您创建 2 个几乎相同的 CAShapeLayers。每个分隔符颜色。您将这些颜色作为 CellGroupedBackgroundView 的属性公开。
根据您设置的 position 属性设置这些图层的路径(仅使用 CGPathAddLineToPoint 用于中间单元格,CGPathAddArcToPoint 用于第一个和最后一个单元格)
2/ 使用在普通 UITableView 上创建的自定义背景视图,根据其 indexPath 设置每个单元格的“位置”...
祝你好运:)
【讨论】: