2016-04-05 34 views
6

之間的空間我想給每個表視圖單元10像素的空間。我可以給UITableViewCell

我該怎麼做?

現在所有的細胞都沒有任何空間

enter image description here

來這裏是我的細胞功能

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *cellIdentifier = @"MyOrderCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    NSLog(@"%d",indexPath.row); 


if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    cell.backgroundColor = [UIColor clearColor]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
} 



UILabel *orderid = (UILabel*)[cell.contentView viewWithTag:101]; 
orderid.textColor = kMaroonColor; 
orderid.font = [UIFont fontWithName:kFontName size:kProductFont]; 
orderid.text = [NSString stringWithFormat:@"ORDER ID : %@",contentDict[@"order_id"]]; 

UILabel *date = (UILabel*)[cell.contentView viewWithTag:102]; 
date.textColor = kMaroonColor; 
date.font = [UIFont fontWithName:kFontName size:kProductFont]; 

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
NSDate *date1 = [dateFormat dateFromString:contentDict[@"order_date"]]; 

// Convert date object to desired output format 
[dateFormat setDateFormat:kDateFormat1]; 
NSString *dateStr = [dateFormat stringFromDate:date1]; 
date.text = dateStr; 

NSArray *products = contentDict[@"products"]; 
UILabel *noOfProducts = (UILabel*)[cell.contentView viewWithTag:103]; 
noOfProducts.textColor = kMaroonColor; 
noOfProducts.font = [UIFont fontWithName:kFontName size:kProductFont]; 
noOfProducts.text= [NSString stringWithFormat:@"NO OF PRODUCTS : %d",products.count]; 


NSArray *totalArray = contentDict[@"totals"]; 
NSDictionary *totalDict = [totalArray objectAtIndex:0]; 
UILabel *price = (UILabel*)[cell.contentView viewWithTag:104]; 
price.textColor = [UIColor blackColor]; 
price.font = [UIFont fontWithName:kFontName size:kProductFont]; 
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"\u20B9 %@",totalDict[@"total"]]]; 
[attributeString addAttribute:NSForegroundColorAttributeName 
         value:kGreyColor 
         range:NSMakeRange(0, 1)]; 
price.attributedText = attributeString; 

UIView* shadowView = [[UIView alloc]init]; 
[cell setBackgroundView:shadowView]; 
// border radius 
[shadowView.layer setCornerRadius:10.0f]; 

// border 
[shadowView.layer setBorderColor:[UIColor lightGrayColor].CGColor]; 
[shadowView.layer setBorderWidth:1.5f]; 

// drop shadow 
[shadowView.layer setShadowColor:[UIColor blackColor].CGColor]; 
[shadowView.layer setShadowOpacity:0.8]; 
[shadowView.layer setShadowRadius:3.0]; 
//[cell.layer setShadowOffset:CGSizeMake(5.0, 5.0)]; 
[tableView setSeparatorInset:UIEdgeInsetsMake(10, 10, 10, 10)]; 
[cell setLayoutMargins:UIEdgeInsetsMake(10, 10, 10, 10)]; 
// [tableView setIndentationWidth:10]; 
// [tableView setIndentationLevel:2]; 

//tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine; 
return cell; 
} 
+0

[當您詢問有關由您的代碼引起的問題的問題時,如果您提供可用於重現問題的代碼,您將得到更好的答案](http://stackoverflow.com/help/mcve) – swiftBoy

+1

當然,讓我們我通過代碼 –

+0

使單元格10px更高,並將內容向下移動,如果你只想在頂部添加頁眉視圖上的空間 – gvuksic

回答

6

最簡單的辦法是讓你的細胞高度有點大(3點從頂部和底部7pt)比你的細胞的背景圖像,並使顏色爲[UIColor clearColor]。這會給人一種錯覺,認爲細胞之間有10微米的距離。

+0

它是否可以與自動佈局一起工作,我想同時給出背景陰影以及空間必須在陰影結束後纔會出現,不包括陰影 –

+1

是的,你可以做到這一點,隨着圖像創建陰影。在圖像之後,放置填充。 –

1

如果想要一個簡單的解決方案,您可以爲所有單元格創建節。每一節每一行。您可以爲節之間的間距添加頁腳高度。

+0

爲每個單元格創建部分看起來不錯,但是它的最後一個解決方案 –

1

其如此簡單 您可以設置自動佈局或調整標籤 以及增加細胞

+0

請添加一些更多信息,這對提問者很容易理解 –

1

只是讓numberOfSections =「你的陣列數」,使各部分的HIGHT的海特只包含一個行。然後定義headerView和它的高度。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return yourArry.count; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 1; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return cellSpacingHeight; 
} 

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView *v = [UIView new]; 
    [v setBackgroundColor:[UIColor clearColor]]; 
    return v; 
} 
+0

我需要同時背景陰影,就像我提供的屏幕截圖 –

+0

ok np call它在CellForRowatIndexPath方法中 –

2

在UITableviewCell-> ContentView中創建一個容器視圖。您將所有子視圖(標籤,按鈕,視圖)保留在該容器視圖中。現在通過設置適當的約束來將容器的邊界設置爲遠離Cell的contentview。

UITableviewCell-> ContentView-> YourCustomContainerView->(全部爲 子視圖)。將YourCustomContainerView的邊界設置爲遠離 ContentView。

+0

如果您使用的是附件視圖,則這不起作用。附件視圖駐留在內容視圖中,不會添加到您的容器視圖中。 –

+0

顯然,這個想法僅適用於自定義單元格。就像與問題分享的人一樣。 – Suresh

+0

自定義單元格仍然可以有附件視圖。 –

2

用戶界面的UITableView設置行高度屬性值210和 在用戶界面的UITableViewCell設置行高度屬性值200

它將在TableView中把每個小區之間10像素空間

3
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
     // number of cells or array count 
     return 10; 
    } 

- (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section 
    { 
     return 1; 
    } 

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
    { 
     // space between cells 
     return 10; 
    } 

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
    { 
     UIView *view = [UIView new]; 
     [view setBackgroundColor:[UIColor clearColor]]; 
     return view; 
    } 
1

增加細胞海特....並隱藏分離器,並設置背景視圖setBackgroundColor:的UIColor clearColor]]

2

我想最好的方法是爲每個正常細胞添加一個分隔符單元格。讓每一種單元都做它自己的工作,並且您可以重用分隔符和正常單元格,並且無需再次更改其UI。你可以自定義分隔符,只要你想,所有的計算很簡單,很容易像這樣:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return dataArray.count * 2; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if (indexPath.row % 2 == 0) { 
     CellForData *cell = [tableView dequeueReusableCellWithIdentifier:@"dataCellID" forIndexPath:indexPath]; 
     return cell; 
    }else{ 
     CellForSeparation *cell = [tableView dequeueReusableCellWithIdentifier:@"separatorCellID" forIndexPath:indexPath]; 
     return cell; 
    } 
} 
0

您可以通過在每個部分&集的tableview風格1個細胞Grouped實現這一目標。

0

如果您希望能夠創建部分並且不想在單元格內創建神祕空間,請創建一個集合視圖而不是表視圖。集合視圖可以設置爲僅限垂直,僅限一列=您還可以設置最小行高和項目間距。Apple Developer Docs for UICollectionView

0

將UIView(parentView)添加到ContentView。從頂部和底部向parentView添加10個像素間距。在parentView中添加你的單元格內容。

相關問題