在項目開發中我們會常常遇到tableView 的cell分割線顯示不全,左邊會空出一截像素,更有甚者想改變系統的分割線,並且只要上下分割線的一個等等需求,今天重點解決以上需求。
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));
}
2.如何解決iOS headerview與tableview之間距離控制?
view 作為 tableView 的 tableHeaderView,單純的改變 view 的 frame 是無濟於事的,tableView不會大度到時刻適應它的高度(以後 Apple 會不會改變就不知道了), 所以,如何告訴tableView 它的 tableHeaderView 已經改變了?很簡單,就一句話(關鍵最後一句):
[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]];
}
}
5.如何解決UILabel 標簽後補空格失效 或間隔打空格只顯示一個空格?
iOS7.0以後的UILabel會自動將Text行尾的空白字符全部去除,除了常見的半角空格(\0×20)和制表符(\t)之外,全角空格 (\u3000)也被計算在內,甚至連多余的換行符(\r,\n)也被自動去除了。 這一點雖然方便直接將控件賦值和無需取值後再trim,但是太過智能化 了之後,往往不能滿足一些本可以簡單實現的需求。
需求1.使用添加\n方式將上下文本連續空兩行,即實現文本的2倍行距。
附錄:
UILabel會自動清除的空白字符(UNICODE)
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 的數組
}