Reputation: 107
I was finding a way to add space between two UItableviewcells Like in facebook and found a solution kindly check the below code
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.contentView.backgroundColor = [UIColor whiteColor];
UIView *whiteRoundedView = [[UIView alloc]initWithFrame:CGRectMake(8, 8, self.view.frame.size.width-18, cell.contentView.frame.size.height - 18)];
CGFloat colors[]={1.0,1.0,1.0,1.0};//cell color white
whiteRoundedView.layer.backgroundColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(), colors);
whiteRoundedView.layer.masksToBounds = false;
whiteRoundedView.layer.cornerRadius = 5.0;
whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, 1);
whiteRoundedView.layer.shadowOpacity = 0.3;
whiteRoundedView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
[cell.contentView addSubview:whiteRoundedView];
[cell.contentView sendSubviewToBack:whiteRoundedView];}
this code is giving me the output I want but the issue is It calls again and again and makes my app slow as I scroll the tablview and shadow is also keep on increasing as I scroll down Can anyone help me with this issue ?
Upvotes: 0
Views: 271
Reputation: 77460
TableViewCells are reused - if you have a lot of rows, they are reused over and over and over. Each time willDisplayCell:
is called, you are adding another whiteRoundedView
. So, after you scroll a bit and a cell has been reused 10 times, you've added 10 additional subviews. Scroll for a while and now you've got dozens of added subviews. And you're doing that for every row.
You can either:
a) check for the existence of the added subview, and only add it if it's the first time, or...
b) you can create a custom UITableViewCell
and format it however you want (much better option).
Upvotes: 3
Reputation: 437
Just add one view in cell with 20 pixel less height compare to cell and set y position of 10
Set cell background color clear and set view color as you cell background color
Upvotes: 1