2017年1月5日
一.如何實(shí)現(xiàn)微信列表(tableView)分割線效果:除最后一個(gè)cell的分割線不偏移子寓,其它c(diǎn)ell分割線都偏移15
(默認(rèn)分割線和自定義分割線,原理其實(shí)類(lèi)似笋除。本例由于用的是點(diǎn)三方庫(kù)文件斜友,所以是默認(rèn)分割線)<我們一貫保留不到萬(wàn)不得已不修改第三方庫(kù)代碼的原則
>)
效果如下:
Paste_Image.png
法1(推薦):直接在tableview將要顯示的接口(不建議在畫(huà)cell的接口地方修改,可能會(huì)有多種cell類(lèi)型判斷)
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [self.recentSessions count] - 1) {
//如果是最后一個(gè)垃它,不偏移
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
[cell setSeparatorInset:UIEdgeInsetsZero];
}
}else{
//其他 還原(其實(shí)ios8 默認(rèn)會(huì)有15的分割線)
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsMake(0, 15, 0, 0)];
}
if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
[cell setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 0)];
}
}
}
法2:通過(guò)移除最后一個(gè)cell的分割線鲜屏,添加底部視圖實(shí)現(xiàn)」矗【自定義分割線的cell可以用如下方法(系統(tǒng)默認(rèn)的還沒(méi)試成功)】
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0.5;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *bgv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, HHBWIDTH, 0.5)];
bgv.backgroundColor = [UIColor lightGrayColor];
return bgv;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [self.recentSessions count] - 1) {
//如果是最后一個(gè)洛史,
//自定義cell隱藏分割線
[self hideSeparator];
}else{
//其他 還原(其實(shí)ios8 默認(rèn)會(huì)有15的分割線,)http://stackoverflow.com/questions/25770119/ios-8-uitableview-separator-inset-0-not-working
[self resetSeparator];
}
}
- (void)hideSeparator
{}
- (void)resetSeparator
{}
如果您發(fā)現(xiàn)本文對(duì)你有所幫助酱吝,如果您認(rèn)為其他人也可能受益也殖,請(qǐng)把它分享出去。