如图:所示
OC纯代码布局
TableviewCell 的两端并没有对齐屏幕(箭头中指的线就是屏幕的边框)
还有就是上面三个cell.
第0行的左上
和右上
有圆角.第1行的是没有圆角
的.第2行的左下
和右下
有圆角
这样的效果怎么封装?
快快快,代码贴出来啊...谢谢啦...
如图:所示
OC纯代码布局
TableviewCell 的两端并没有对齐屏幕(箭头中指的线就是屏幕的边框)
还有就是上面三个cell.
第0行的左上
和右上
有圆角.第1行的是没有圆角
的.第2行的左下
和右下
有圆角
这样的效果怎么封装?
快快快,代码贴出来啊...谢谢啦...
我之前这种排列也可以不用UITableViewCell,而是直接用自定义UIView.外面整个有圆角,里面放两个按钮即可。
假如用UITableViewCell,那么其圆角最好是UI给带圆角的背景图,然后自定义里面的视图就行。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
这个方法比layoutSubviews方法先调用,以至于我在layoutSubviews方法修改了Cell的宽度.
#import "LMSMeViewCell.h"
@implementation LMSMeViewCell
- (void)layoutSubviews {
[super layoutSubviews];
//修改cell的frame
CGRect rect = self.frame;
//如果已经修改过就不用修改了
if (self.frame.size.width == SCREEN_WIDTH) {
NSLog(@"%f",SCREEN_WIDTH);
rect = CGRectMake(self.frame.origin.x + 10, self.frame.origin.y, self.frame.size.width - 20, self.frame.size.height);
}
self.frame = rect;
NSLog(@"**********%@",NSStringFromCGRect(rect));
self.textLabel.font = [UIFont systemFontOfSize:13];
self.textLabel.textColor = [UIColor colorWithRed:0.51 green:0.51 blue:0.51 alpha:1];
[self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(10);
make.width.height.offset(22);
make.centerY.equalTo(self.contentView);
}];
[self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.imageView.mas_right).offset(20);
make.centerY.equalTo(self.imageView.mas_centerY);
}];
}
@end
Controller关键代码:
#pragma mark- cell 将要显示的时候调用这个方法,就在这个方法内进行圆角绘制
/**
本质:就是修改cell的背景view,这个view的layer层自己分局cell的类型(顶,底和中间)来绘制*/
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"___");
if ([cell respondsToSelector:@selector(tintColor)])
{
CGFloat cornerRadius = 6.f;//圆角大小
cell.backgroundColor = [UIColor clearColor];
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
CGMutablePathRef pathRef = CGPathCreateMutable();
CGRect bounds = CGRectInset(cell.bounds, 0, 0);
NSLog(@"___%@",NSStringFromCGRect(bounds));
// CGRect bounds = CGRectInset(cell.frame, 0, 0);
BOOL addLine = NO;
if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1)
{
CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
}
else if (indexPath.row == 0)
{ //最顶端的Cell(两个向下圆弧和一条线)
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
addLine = YES;
}
else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1)
{ //最底端的Cell(两个向上的圆弧和一条线)
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
}
else
{ //中间的Cell
CGPathAddRect(pathRef, nil, bounds);
addLine = YES;
}
layer.path = pathRef;
CFRelease(pathRef);
layer.fillColor = [UIColor orangeColor].CGColor; //cell的填充颜色
// layer.strokeColor = [UIColor lightGrayColor].CGColor; //cell 的边框颜色
layer.strokeColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1].CGColor;
if (addLine == YES) {
CALayer *lineLayer = [[CALayer alloc] init];
CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
lineLayer.frame = CGRectMake(CGRectGetMinX(bounds), bounds.size.height-lineHeight, bounds.size.width, lineHeight);
lineLayer.backgroundColor = [UIColor lightGrayColor].CGColor; //绘制中间间隔线
[layer addSublayer:lineLayer];
}
UIView *bgView = [[UIView alloc] initWithFrame:bounds];
[bgView.layer insertSublayer:layer atIndex:0];
bgView.backgroundColor = UIColor.clearColor;
cell.backgroundView = bgView;
}
}
2 回答1.1k 阅读
1 回答987 阅读✓ 已解决
1 回答2.7k 阅读
1 回答1.4k 阅读
1.7k 阅读
1 回答1.1k 阅读
1.3k 阅读
圆角的cell,之前iOS 6的时候是这种的(拟物),iOS 7 (扁平化)之后就进行了修改!
主要的实现在tableView的代理方法中 - (void)tableView:willDisplayCell: forRowAtIndexPath: ,绘制table view cell 的背景view。
下面的代码,你可以直接拷贝到控制器中演示效果,如果要集成到你自己的代码中,你需要把 - (void)tableView:willDisplayCell: forRowAtIndexPath: 这个方法中的东西拷贝过去,还要修改table view 的样式是分组和分割线为 none,这些在 - viewDidLoad中你也可以找到。
效果图:
