2017年1月5日
一.如何實現(xiàn)微信列表(tableView)分割線效果:除最后一個cell的分割線不偏移,其它cell分割線都偏移15
(默認分割線和自定義分割線,原理其實類似。本例由于用的是點三方庫文件,所以是默認分割線)<我們一貫保留不到萬不得已不修改第三方庫代碼的原則>)
效果如下:

Paste_Image.png
法1(推薦):直接在tableview將要顯示的接口(不建議在畫cell的接口地方修改,可能會有多種cell類型判斷)
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [self.recentSessions count] - 1) {
//如果是最后一個,不偏移
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
[cell setSeparatorInset:UIEdgeInsetsZero];
}
}else{
//其他 還原(其實ios8 默認會有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:通過移除最后一個cell的分割線,添加底部視圖實現(xiàn)?!咀远x分割線的cell可以用如下方法(系統(tǒng)默認的還沒試成功)】
-(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) {
//如果是最后一個,
//自定義cell隱藏分割線
[self hideSeparator];
}else{
//其他 還原(其實ios8 默認會有15的分割線,)http://stackoverflow.com/questions/25770119/ios-8-uitableview-separator-inset-0-not-working
[self resetSeparator];
}
}
- (void)hideSeparator
{}
- (void)resetSeparator
{}
如果您發(fā)現(xiàn)本文對你有所幫助,如果您認為其他人也可能受益,請把它分享出去。