2017-05-26 64 views
0

我是找到一種方式,Facebook的2個UItableviewcells之間添加空間一樣,發現了一個解決方案查收下面的代碼目標C - 兩個tableview中細胞間給予空間IOS

-(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];} 

這段代碼是給我的輸出我想,但問題是 它一次又一次地呼籲,使我的應用程序緩慢,因爲我滾動tablview 和陰影也在不斷增加,我向下滾動 誰能幫助我解決這個問題?

+1

你可以在UITableViewCell的底部添加的UIView,這將不會影響性能。 –

+1

選中此:https://stackoverflow.com/questions/17490203/how-to-create-paddingspacing-between-rows-in-tablview/17490283#17490283 – brianLikeApple

回答

3

TableViewCells被重用 - 如果你有很多行,它們會被反覆使用。每次調用willDisplayCell:時,都會添加另一個whiteRoundedView。所以,在你滾動一下並且單元格被重用了10次之後,你已經添加了10個子視圖。滾動了一段時間,現在你已經有了幾十添加子視圖。並且您正在爲每行

您可以:

一)檢查所添加的子視圖的存在,只有當它的第一次,或將其添加...

b)您可以創建自定義UITableViewCell和格式化它,但你想(更好的選擇)。

1

細胞只需添加一個視圖與20像素較少高度比較10

設置單元格背景顏色清晰,並設置視圖顏色的細胞,並設置y座標爲您單元格的背景顏色