ios 我可以在 UITableViewCell 之间留出空间吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36418492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:52:01  来源:igfitidea点击:

Can I give space between UITableViewCell

iosobjective-ciphoneuitableview

提问by Tarun Seera

I would like to give space of 10 pixel in each table view cell.

我想在每个表格视图单元格中留出 10 像素的空间。

How I can do that?

我怎么能做到这一点?

Right now all cells are coming without any space

现在所有单元格都没有任何空间

enter image description here

在此处输入图片说明

Here is my cell function

这是我的细胞功能

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

回答by Saheb Roy

The most simple way would be make your cells height a bit bigger(3pt from top and 7pt from the bottom) than that of your cell's background image, and making the colour as [UIColor clearColor]. That would give the illusion that the cells have a 10 pt gap in between.

最简单的方法是使您的单元格高度比单元格背景图像的高度大一点(顶部 3pt,底部 7pt),并将颜色设为[UIColor clearColor]. 这会给人一种错觉,即细胞之间有 10 pt 的间隙。

回答by Himanshu jamnani

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

回答by Yagnesh Dobariya

In the user interface for UITableView set Row Height Attribute value 210. and In the user interface for UITableViewCell set Row Height Attribute value 200.

在 UITableView 的用户界面中设置 Row Height 属性值 210。 在 UITableViewCell 的用户界面中设置 Row Height 属性值 200。

It will put 10 px space between each cell in the TableView

它将在 TableView 中的每个单元格之间放置 10 px 的空间

回答by Abhinandan Pratap

just make numberOfSections = "Your array count" and make each section contains only one row. And then define headerView and its height.

只需使 numberOfSections = "Your array count" 并使每个部分只包含一行。然后定义 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;
}

回答by Suresh Kumar Durairaj

Create a container view inside UITableviewCell->ContentView. You keep all your subviews(labels,buttons,views) inside that container view. Now set container's boundaries away from the Cell's contentview by setting appropriate constraints.

在 UITableviewCell->ContentView 中创建一个容器视图。您将所有子视图(标签、按钮、视图)保存在该容器视图中。现在通过设置适当的约束将容器的边界设置为远离单元格的内容视图。

UITableviewCell->ContentView->YourCustomContainerView-> (all subviews). Set YourCustomContainerView's boundaries away from the ContentView.

UITableviewCell->ContentView->YourCustomContainerView->(所有子视图)。将 YourCustomContainerView 的边界设置为远离 ContentView。

回答by MudOnTire

I guess the best way is to add a separator cell for each normal cell. let each kind of cell do its own job, and you can reuse the the separator and normal cell else where and need not to change their UI again. and you can customize the separator as you want, and all the calculations are easy, easy like this:

我想最好的方法是为每个正常单元格添加一个分隔符单元格。让每种单元格做自己的工作,您可以在其他地方重复使用分隔符和普通单元格,而无需再次更改它们的 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;
    }
}

回答by Chetan

If for a simple solution you can create sections for all cells. Each section each row. You can add a footer height for spacing between sections.

如果对于一个简单的解决方案,您可以为所有单元格创建部分。每节每行。您可以为节之间的间距添加页脚高度。

回答by fathima

its so simple you can set the auto layout or adjust the Hight of the label as well increases the hight of the cell

它是如此简单,您可以设置自动布局或调整标签的高度以及增加单元格的高度

回答by fathima

Increase the cell Hight.... And hide the separator and set the background view setBackgroundColor:[UIColor clearColor]]

增加单元格高度....并隐藏分隔符并设置背景视图 setBackgroundColor:[UIColor clearColor]]

回答by Matthieu Riegler

You can achieve that by having 1 cell per section & set tableview style to Grouped.

您可以通过每个部分有 1 个单元格并将 tableview 样式设置为Grouped.