在项目开发中我们会常常遇到tableView 的cell分割线显示不全,左边会空出一截像素,更有甚者想改变系统的分割线,并且只要上下分割线的一个等等需求,今天重点解决以上需求,仅供参考:
每日更新关注:http://weibo.com/hanjunqiang
新浪微博!
1.cell 分割线不全:
解决方案1:
补全分割线
-(void)viewDidLayoutSubviews {
if ([_listTableView respondsToSelector:@selector(setSeparatorInset:)]) {
[_listTableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([_listTableView respondsToSelector:@selector(setLayoutMargins:)]) {
[_listTableView 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];
}
}UITableViewCell绘制分割线
//第一步:
//UITableView去掉自带系统的分割线
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//第二步:
//在自定义的UITableViewCell里重写drawRect:方法
#pragma mark - 绘制Cell分割线
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, rect);
//上分割线,
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:198/255.0 green:198/255.0 blue:198/255.0 alpha:1].CGColor);
CGContextStrokeRect(context, CGRectMake(0, 0, rect.size.width, 1));
//下分割线
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:198/255.0 green:198/255.0 blue:198/255.0 alpha:1].CGColor);
CGContextStrokeRect(context, CGRectMake(0, rect.size.height, rect.size.width, 1));
}
每日更新关注:http://weibo.com/hanjunqiang
新浪微博!
[webView sizeToFit]; CGRect newFrame = headerView.frame; newFrame.size.height = newFrame.size.height + webView.frame.size.height; headerView.frame = newFrame; [self.tableView setTableHeaderView:headerView];
//这样以后,效果就出来了。不过这种过度显得有些生硬,能不能加一点点动画,让它变得顺眼一些呢?试试下面的代码: [self.tableView beginUpdates]; [self.tableView setTableHeaderView:headerView]; [self.tableView endUpdates];
//改变cell的选中颜色: cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame]; cell.selectedBackgroundView.backgroundColor = COLOR_BACKGROUNDVIEW;
// 默认选中第一行
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
// 实现了选中第一行的方法
[self tableView:_mainIndustryTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
//例如:
// 默认下选中状态
- (void)customAtIndex:(UITableView *)tableView
{
// 默认选中第一行
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
if ([tableView isEqual:_mainIndustryTableView]) {
[self tableView:tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
}
}
每日更新关注:http://weibo.com/hanjunqiang
新浪微博!
6.自定义滑动删除背景及字体颜色如图:
//1.自定义滑动删除背景及字体颜色
//2. 然后实现另一个代理方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
editingStyle = UITableViewCellEditingStyleDelete;//此处的EditingStyle可等于任意 UITableViewCellEditingStyle,该行代码只在iOS8.0以前版本有作用,也可以不实现。
}
//3. 再实现
-(NSArray )tableView:(UITableView )tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@”删除” handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {//title可自已定义
NSLog(@”点击删除”);
}];//此处是iOS8.0以后苹果最新推出的api,UITableViewRowAction,Style是划出的标签颜色等状态的定义,这里也可自行定义
UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"编辑" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
}];
editRowAction.backgroundColor = [UIColor colorWithRed:0 green:124/255.0 blue:223/255.0 alpha:1];//可以定义RowAction的颜色
return @[deleteRoWAction, editRowAction];//最后返回这俩个RowAction 的数组
}
如有问题可关注微博咨询博主,提出更好的建议!
每日更新关注:http://weibo.com/hanjunqiang
新浪微博!
原文:http://blog.csdn.net/qq_31810357/article/details/51474446