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

來源:互聯網
上載者:User

標籤:style   blog   color   使用   os   io   for   問題   

對於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)); }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.