iOS11 SectionHeader 胡乱移动且滑动时出现重复内容的解决方法
升级到iOS11后,痛苦的事情多起来了,以前版本没有的出现问题的代码,经过Xcode9一编译,千万草泥马奔腾而过;
今天碰到一个奇葩问题,直接进入主题:
问题描述:
-(CGFloat)tableView:(UITableView*)tableViewheightForHeaderInSection:(NSInteger)section { return12; } -(UIView*)tableView:(UITableView*)tableViewviewForHeaderInSection:(NSInteger)section { UIView*headerSection_V=[[UIViewalloc]initWithFrame:CGRectMake(ZERODIS,ZERODIS,SCREEN_WIDTH,12)]; [headerSection_VsetBackgroundColor:COLOR_3]; returnheaderSection_V; }
1- headerView会错乱移动,且调整tableView的style也没有效果;
2-滑动tableView的时候,貌似底部又多出一个图层tableView,重复了tableViewCell的内容;
3- 以下代码无效:(当然tableVIew懒加载的时候还有相应代码设置cell分割线的偏移)
/** *解决cell分割线距离两边12居中对齐 */ -(void)tableView:(UITableView*)tableViewwillDisplayCell:(UITableViewCell*)cellforRowAtIndexPath:(NSIndexPath*)indexPath { if([cellrespondsToSelector:@selector(setSeparatorInset:)]){ [cellsetSeparatorInset:UIEdgeInsetsMake(ZERODIS,12,ZERODIS,12)]; } if([cellrespondsToSelector:@selector(setLayoutMargins:)]){ [cellsetLayoutMargins:UIEdgeInsetsMake(ZERODIS,12,ZERODIS,12)]; } }
最后排查发现:
旧代码使用了xib但是又没有用xib的tableView, tableView又是自己代码生成的,把xib删除之后,就OK了;
PS:下面通过实例代码给大家分享UITableViewSectionHeader自定义section的头部。
具体代码如下所示:
//自定义section的头部 -(UIView*)tableView:(UITableView*)tableViewviewForHeaderInSection:(NSInteger)section { UIView*headerView=[[UIViewalloc]initWithFrame:CGRectMake(10,0,300,30)];//创建一个视图 UIImageView*headerImageView=[[UIImageViewalloc]initWithFrame:CGRectMake(10,0,300,30)]; UIImage*image=[UIImageimageNamed:@"4-2.png"]; [headerImageViewsetImage:image]; [headerViewaddSubview:headerImageView]; [headerImageViewrelease]; NSString*createTime=[self.keysArrayobjectAtIndex:section]; createTime=[createTimestringByReplacingCharactersInRange:NSMakeRange(4,1)withString:@"-"]; createTime=[createTimestringByReplacingCharactersInRange:NSMakeRange(7,1)withString:@"-"]; UILabel*headerLabel=[[UILabelalloc]initWithFrame:CGRectMake(130,5,150,20)]; headerLabel.backgroundColor=[UIColorclearColor]; headerLabel.font=[UIFontboldSystemFontOfSize:15.0]; headerLabel.textColor=[UIColorblueColor]; headerLabel.text=createTime; [headerViewaddSubview:headerLabel]; [headerLabelrelease]; returnheaderView; }
总结
以上所述是小编给大家介绍的iOS11SectionHeader胡乱移动且滑动时出现重复内容的解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!