每日更新关注:http://weibo.com/hanjunqiang
在开发中经常遇到cell分割线显示不全或者想自定义线的宽高等; 最近总结了一下,希望帮到大家:
1.不想划线怎么办?
TableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; // 设置系统默认线的样式
-(void)viewDidLayoutSubviews {
if ([TableView respondsToSelector:@selector(setSeparatorInset:)]) {
[TableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([TableView respondsToSelector:@selector(setLayoutMargins:)]) {
[TableView setLayoutMargins:UIEdgeInsetsZero];
}
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
[cell setSeparatorInset:UIEdgeInsetsZero];
}
}
每日更新关注:http://weibo.com/hanjunqiang
2.想划线设置怎么办?
TableView.separatorStyle = UITableViewCellSeparatorStyleNone; // 丢掉系统的线,画自定义的线
#define SINGLE_LINE_HEIGHT (1/[UIScreen mainScreen].scale) // 线的高度
#define COLOR_LINE_GRAY [UIColor colorWithRed:224/255.0f green:224/255.0f blue:224/255.0f alpha:1] //分割线颜色 #e0e0e0
在自定义cell里写入:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, rect);
//下分割线
CGContextSetStrokeColorWithColor(context, COLOR_LINE_GRAY.CGColor); // COLOR_LINE_GRAY 为线的颜色
CGContextStrokeRect(context, CGRectMake(0, rect.size.height, rect.size.width, SINGLE_LINE_HEIGHT)); //SINGLE_LINE_HEIGHT 为线的高度
}
每日更新关注:http://weibo.com/hanjunqiang iOS开发者交流QQ群:446310206 有问题或技术交流可以咨询!欢迎加入!