iOS開發中tableViewCell的懸浮效果


其實很早就想寫博客了,只是感覺自己的技術不行,寫出來的東西技術性不強,寫出來也沒什么用,不過后來想想,寫寫博客記錄開發中遇到的問題也不錯....

今天我想寫的是tableView的懸浮效果,因為我們公司最近在開發社區,就是和百度貼吧類似的,嵌套在應用中,而其中關於每一個的帖子要像這種效果

開始是做不出這種效果,就直接寫個tableView算了,后來跟問了下做安卓的那邊,原來是陰影的效果...

說到這里,相信好多同學都知道,好吧,我們開始上代碼,首先是創建tableView,這個就不用多說了吧,這是創建tableView的.m文件

#import "ViewController.h"
#import "DemoCell.h"

//屏幕寬度
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
//屏幕高度
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)

@interface ViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,assign)CGFloat height;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTabelView];
}
- (void)createTabelView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT-20) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //注意重用
    static NSString *cellId = @"cell";
    DemoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if(cell == nil){
        cell = [[DemoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [DemoCell getHeight] + 10;
}
@end

然后是自定義cell,自定義cell要注意因為要寫出懸浮效果,所以我想的是給Cell加一層View,這個View的frame比Cell的contentView小一圈,在設置邊框的陰影,就能寫出懸浮效果了,這是自定義Cell的.m文件

#import "DemoCell.h"

#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
 

@interface DemoCell()
@property (nonatomic,strong)UIView *bgView;
@end

@implementation DemoCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self createUI];
    }
    return self;
}
//重點在這里
- (void)createUI{
    //創建一個UIView比self.contentView小一圈
    self.bgView  = [[UIView alloc] initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH - 20, 100)];
    self.bgView.backgroundColor = [UIColor whiteColor];
    //給bgView邊框設置陰影
    self.bgView.layer.shadowOffset = CGSizeMake(1,1);
    self.bgView.layer.shadowOpacity = 0.3;
    self.bgView.layer.shadowColor = [UIColor blackColor].CGColor;
    [self.contentView addSubview:self.bgView];
}
+ (CGFloat)getHeight{
    //在這里能計算高度,動態調整
    return 100;
}
@end

運行起來的效果圖,是不是棒棒噠

 結束,第一篇博客就寫到這里了,就算沒人看(還是希望有人看的),我也會寫下去的哦...

 


免責聲明!

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



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