iOS開發(fā) - UITableView 卡片效果,同時(shí)給UITableView的header街图,footer, row 添加陰影

項(xiàng)目中有卡片效果顯示的樣式餐济,即一個(gè)表格呈卡片狀,有陰影及邊框絮姆。

  • 思路:將 UITableView 的每個(gè) section 統(tǒng)一設(shè)置成卡片效果篙悯,即同時(shí)給 UITableView 的 header,footer, cell 添加陰影及邊框鸽照。進(jìn)一步說矮燎,就是給 tableViewHeader 的左上右部分添加 view,給 tableViewFooter 的左下右部分添加 view澜沟,給 cell 的左右部分添加 view峡谊,最后給這些 view 全部添加漸變色。
  • 效果圖
卡片效果圖
  • 核心代碼:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, headerViewHeight)];
    view.backgroundColor = [UIColor whiteColor];
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(2, 4, 6, headerViewHeight-4)];
    [view addSubview:leftView];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, headerViewHeight)];
    label.text = [NSString stringWithFormat:@"       組頭%ld", section+1];
    [view addSubview:label];
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = leftView.bounds;
    gradient.colors = [NSArray arrayWithObjects:
                       (id)[UIColor colorWithRed:(0/255.0)  green:(0/255.0)  blue:(0/255.0)  alpha:0.0].CGColor,
                       (id)[UIColor colorWithRed:(0/255.0)  green:(0/255.0)  blue:(0/255.0)  alpha:0.12].CGColor, nil];
    gradient.startPoint = CGPointMake(0, 0);
    gradient.endPoint = CGPointMake(1, 0);
    [leftView.layer addSublayer:gradient];
    
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth - 8, 4, 6, headerViewHeight-4)];
    [view addSubview:rightView];
    CAGradientLayer *gradientR = [CAGradientLayer layer];
    gradientR.frame = rightView.bounds;
    gradientR.colors = [NSArray arrayWithObjects:
                        (id)[UIColor colorWithRed:(0/255.0)  green:(0/255.0)  blue:(0/255.0)  alpha:0.12].CGColor,
                        (id)[UIColor colorWithRed:(0/255.0)  green:(0/255.0)  blue:(0/255.0)  alpha:0.0].CGColor, nil];
    gradientR.startPoint = CGPointMake(0, 0);
    gradientR.endPoint = CGPointMake(1, 0);
    [rightView.layer addSublayer:gradientR];
    
    UIView *topV = [[UIView alloc] initWithFrame:CGRectMake(6, 2, kScreenWidth - 12, 2)];
    [view addSubview:topV];
    CAGradientLayer *gradientT = [CAGradientLayer layer];
    gradientT.frame = topV.bounds;
    gradientT.colors = [NSArray arrayWithObjects:
                        (id)[UIColor colorWithRed:(0/255.0)  green:(0/255.0)  blue:(0/255.0)  alpha:0.12].CGColor,
                        (id)[UIColor colorWithRed:(0/255.0)  green:(0/255.0)  blue:(0/255.0)  alpha:0.0].CGColor, nil];
    gradientT.startPoint = CGPointMake(0, 1);
    gradientT.endPoint = CGPointMake(0, 0);
    [topV.layer addSublayer:gradientT];
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, footerViewHeight)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, footerViewHeight)];
    label.text = [NSString stringWithFormat:@"       組尾%ld", section+1];
    [view addSubview:label];
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(2, 0, 6, footerViewHeight-4)];
    [view addSubview:leftView];
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = leftView.bounds;
    gradient.colors = [NSArray arrayWithObjects:
                       (id)[UIColor colorWithRed:(0/255.0)  green:(0/255.0)  blue:(0/255.0)  alpha:0.12].CGColor,
                       (id)[UIColor colorWithRed:(0/255.0)  green:(0/255.0)  blue:(0/255.0)  alpha:0.0].CGColor, nil];
    gradient.startPoint = CGPointMake(1, 0);
    gradient.endPoint = CGPointMake(0, 0);
    [leftView.layer addSublayer:gradient];
    
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth - 8, 0, 6, footerViewHeight-4)];
    [view addSubview:rightView];
    CAGradientLayer *gradientR = [CAGradientLayer layer];
    gradientR.frame = rightView.bounds;
    gradientR.colors = [NSArray arrayWithObjects:
                        (id)[UIColor colorWithRed:(0/255.0)  green:(0/255.0) blue:(0/255.0)  alpha:0.12].CGColor,
                        (id)[UIColor colorWithRed:(0/255.0)  green:(0/255.0)  blue:(0/255.0)  alpha:0.0].CGColor, nil];
    gradientR.startPoint = CGPointMake(0, 0);
    gradientR.endPoint = CGPointMake(1, 0);
    [rightView.layer addSublayer:gradientR];
    
    UIView *bottomV = [[UIView alloc] initWithFrame:CGRectMake(6, footerViewHeight-4, kScreenWidth - 12, 2)];
    [view addSubview:bottomV];
    CAGradientLayer *gradientB = [CAGradientLayer layer];
    gradientB.frame = bottomV.bounds;
    gradientB.colors = [NSArray arrayWithObjects:
                        (id)[UIColor colorWithRed:(0/255.0)  green:(0/255.0) blue:(0/255.0)  alpha:0.12].CGColor,
                        (id)[UIColor colorWithRed:(0/255.0)  green:(0/255.0) blue:(0/255.0)  alpha:0.0].CGColor, nil];
    gradientB.startPoint = CGPointMake(0, 0);
    gradientB.endPoint = CGPointMake(0, 1.0);
    [bottomV.layer addSublayer:gradientB];
    return view;
}

  • 自定義的cell中的demo:
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(2, 0, 6, cellHeight)];
        [self addSubview:leftView];
        CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.frame = leftView.bounds;
        // 漸變色數(shù)組
        gradient.colors = [NSArray arrayWithObjects:
                           (id)[UIColor colorWithRed:(0/255.0)  green:(0/255.0)  blue:(0/255.0)  alpha:0.12].CGColor,
                           (id)[UIColor colorWithRed:(0/255.0)  green:(0/255.0)  blue:(0/255.0)  alpha:0.0].CGColor, nil];
        gradient.startPoint = CGPointMake(1, 0);
        gradient.endPoint = CGPointMake(0, 0);
        [leftView.layer addSublayer:gradient];
        
        UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth - 8, 0, 6, cellHeight)];
        [self addSubview:rightView];
        
        CAGradientLayer *gradientR = [CAGradientLayer layer];
        gradientR.frame = rightView.bounds;
        // 漸變色數(shù)組
        gradientR.colors = [NSArray arrayWithObjects:
                            (id)[UIColor colorWithRed:(0/255.0)  green:(0/255.0)  blue:(0/255.0)  alpha:0.12].CGColor,
                            (id)[UIColor colorWithRed:(0/255.0)  green:(0/255.0)  blue:(0/255.0)  alpha:0.0].CGColor, nil];
        gradientR.startPoint = CGPointMake(0, 0);
        gradientR.endPoint = CGPointMake(1, 0);
        [rightView.layer addSublayer:gradientR];
        
        self.testLabel = [UILabel new];
        [self addSubview:self.testLabel];
        self.testLabel.frame = self.frame;
    }
    return self;
}
  • demo地址,歡迎各位同行一起交流學(xué)習(xí)馒索。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末绰上,一起剝皮案震驚了整個(gè)濱河市渠驼,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖爽哎,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件课锌,死亡現(xiàn)場(chǎng)離奇詭異祈秕,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)请毛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門方仿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人玻孟,你說我怎么就攤上這事鳍征∶婊眨” “怎么了?”我有些...
    開封第一講書人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵氮双,是天一觀的道長(zhǎng)霎匈。 經(jīng)常有香客問我,道長(zhǎng)暖释,這世上最難降的妖魔是什么墨吓? 我笑而不...
    開封第一講書人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮亮曹,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘式矫。我一直安慰自己役耕,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開白布氏义。 她就那樣靜靜地躺著图云,像睡著了一般。 火紅的嫁衣襯著肌膚如雪克婶。 梳的紋絲不亂的頭發(fā)上丹泉,一...
    開封第一講書人閱讀 51,287評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音筋岛,去河邊找鬼晒哄。 笑死,一個(gè)胖子當(dāng)著我的面吹牛柒傻,可吹牛的內(nèi)容都是我干的较木。 我是一名探鬼主播,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼预侯,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼泳赋!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起校坑,我...
    開封第一講書人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎膏斤,沒想到半個(gè)月后邪驮,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡沮榜,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年蟆融,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了守呜。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡弥喉,死狀恐怖玛迄,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情藻肄,我是刑警寧澤拒担,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布攻询,位于F島的核電站,受9級(jí)特大地震影響低零,放射性物質(zhì)發(fā)生泄漏拯杠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一雄妥、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧老厌,春花似錦、人聲如沸醋拧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)雀费。三九已至痊焊,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間薄啥,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工刁愿, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留到逊,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓脑题,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親铜靶。 傳聞我的和親對(duì)象是個(gè)殘疾皇子叔遂,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫(kù)組件 SD...
    陽(yáng)明先生_X自主閱讀 15,979評(píng)論 3 119
  • 這是我準(zhǔn)備在千聊分享用的稿子 主題是繪畫中如何運(yùn)用光影提升空間立體感。 首先鄭重提示:這次分享都是基礎(chǔ)內(nèi)容蚕苇,大牛們...
    文雅羊閱讀 569評(píng)論 4 3
  • 突然有四五天的空檔哩掺,趕緊買票帶娃回家看望父母。這一個(gè)月雖然不在工作崗位上涩笤,還是安排很緊張嚼吞,生活就應(yīng)該是這樣盒件,緊湊的...
    李孟宸閱讀 243評(píng)論 1 1
  • 被扼住咽喉的鯨魚 躍起跌下 滔天的浪花在兩邊劈開 大海要被撕裂 撕裂一切方的和圓的 遠(yuǎn)的和近的 撕裂一切整的 整的...
    不吃貓的綠鯉魚閱讀 275評(píng)論 0 4
  • 奈曼旗源于奈曼部誊薄,最早稱“乃蠻”履恩。“奈曼”意為“八”呢蔫。最初由八鄂拓克或和碩形成切心。奈曼旗住民,大約在秦漢時(shí)期就有片吊,元...
    歐溪謐閱讀 1,542評(píng)論 3 3