iOS 關於tableView cell的分割線的一些設置


對於iOS的tableView的cell的分割線,一般我們很少使用不是系統默認的,但是有些項目要求還是要求我們去改變分割線的顏色或者外形以配合整個項目的色調。這個蘋果公司早都為我們想到了。

一、關於分割線的位置。

    分割線的位置就是指分割線相對於tableViewCell.如果我們要根據要求調節其位置,那么在iOS7.0版本以后,提供了一個方法如下:

if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 45, 0, 0)];
        
    }

 UIEdgeInsets 的四個參數分別是相對於cell的上、左、下、右的距離,都是CGFloat型。

二、分割線的顏色及風格:

     a、cell的分割線的顏色不是cell的屬性,它屬於tableView的separatorColor屬性。這樣我們只需要設置屬性值就可以得到所有我們想要的顏色的分割線、

[self.tableView setSeparatorColor:[UIColor clearColor]];

   b、cell的風格:它是tableView 的separatorStyle屬性,系統給我們提供了三種風格在枚舉UITableViewCellSeparatorStyle中定義,分別是

typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
    UITableViewCellSeparatorStyleNone,
    UITableViewCellSeparatorStyleSingleLine,
    UITableViewCellSeparatorStyleSingleLineEtched   // This separator style is only supported for grouped style table views currently
};

   默認的是UITableViewCellSeparatorStyleSingleLine.

三、tableViewCell 分割線自定義:首先要把cell自帶的分割線給去掉,使用如下兩種都行,一是把顏色設置為clearColor,二是風格設置為UITableViewCellSeparatorStyleNone。

     自定義cell分割線大致用到的兩種方法 

   a、把自定義的分割線當成一個View放到cell的contentView上,一定要注意重用問題,所以這個view 要在cell初始化的時候添加上。示例代碼如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"huicellacce"]];
        cell.backgroundColor = [UIColor clearColor];
//        cell.selected = YES;
        UIImageView *imageViewSepE = [[UIImageView alloc]initWithFrame:CGRectMake(47, 49, 200, 1)];
        imageViewSepE.image = [UIImage imageNamed:@"godline"];
        [cell.contentView addSubview:imageViewSepE];

    }
}

  b、比較復雜,用到了底層的框架,

- (void)drawRect:(CGRect)rect { 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor); CGContextFillRect(context, rect); 
CGContextSetStrokeColorWithColor(context, [UIColorcolorWithHexString:@"ffffff"].CGColor);
 CGContextStrokeRect(context, CGRectMake(5, -1, rect.size.width - 10, 1)); //下分割線 
CGContextSetStrokeColorWithColor(context, [UIColor colorWithHexString:@"e2e2e2"].CGColor); CGContextStrokeRect(context, CGRectMake(5, rect.size.height, rect.size.width - 10, 1)); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM