The floating Effect of tableViewCell in iOS development, iostableviewcell
In fact, I wanted to write a blog for a long time. I just felt that my technology was not good. The writing was not very technical and it was useless. But I thought about it later, the problems encountered during blog writing and recording development are also good ....
Today, I want to write about the floating Effect of tableView, because our company is developing the Community recently, which is similar to Baidu Post Bar and nested in applications, in this example, the effect of each post is as follows:
In the beginning, I couldn't do this, so I wrote a tableView directly. Later I asked about how to do Android. It turned out to be a shadow effect...
Speaking of this, I believe many of you know that, okay, let's start with the Code. First, we need to create tableView. This is the. m file for creating tableView.
# Import "ViewController. h "# import" DemoCell. h "// screen width # define SCREEN_WIDTH ([[UIScreen mainScreen] bounds]. size. width) // screen height # 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 {// reuse static NSString * cellId = @ "cell"; DemoCell * cell = [tableView dequeueReusableCellWithIdentifier: cellId]; if (cell = nil) {cell = [[DemoCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: cellId];} return cell;}-(void) didreceivemorywarning {[super idle];}-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {return [DemoCell getHeight] + 10;} @ end
Then there is a custom cell. Note that the floating effect should be written, so I want to add a View to the cell. The frame of the View is a circle smaller than the contentView of the Cell, when you set the shadow of the border, you can write the floating effect. This is the custom Cell. m file
# 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;} // focus on this-(void) createUI {// create a UIView than self. contentView is a small circle of self. bgView = [[UIView alloc] initWithFrame: CGRectMake (10, 5, SCREEN_WIDTH-20,100)]; self. bgView. backgroundColor = [UIColor whiteColor]; // set the shadow self for the bgView border. 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 {// calculate the height here and dynamically adjust return 100;} @ end
Is it great to run it?
Now, the first blog is written here. Even if no one looks at it (or you want to see it), I will write it down...