ios 如何在 tablview 中的行之间创建填充(间距)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17490203/
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 00:14:31  来源:igfitidea点击:

How to create padding(spacing) between rows in tablview

iosobjective-cuitableview

提问by Eli Braginskiy

This isn't a duplicate I read the other questions regarding this and none of them gave me what i needed , I'm creating a table view with custom cell and the style is grouped. I have 1 ImageView inside each row that is smaller then the whole row.How many cells I need to create is somthing i get from the db so I cant use static cells I need to have spacing between the rows,I cant figure out how to do this. I tried making the cell Bigger (320,143) and making it invisible which i managed to do but then when you press outside the image it still acting like i pressed the cell this is pretty much what i want to achieve

这不是重复我阅读了有关此的其他问题,但没有一个给我所需的内容,我正在创建一个带有自定义单元格的表格视图,并且样式已分组。我在每一行中有 1 个 ImageView,它比整行都小。我需要创建多少个单元格是我从数据库中得到的,所以我不能使用静态单元格我需要在行之间留出间距,我不知道如何做这个。我尝试使单元格变大(320,143)并使其不可见,我设法做到了但是当你按下图像外时它仍然表现得像我按下了单元格这几乎是我想要实现的

first pic

第一张照片

but this is what happens:

但这就是发生的事情:

second pic

第二张照片

I need the cell to be the images size but to have spacing between diffrent rows. how can i do this?

我需要单元格是图像大小,但不同行之间有间距。我怎样才能做到这一点?

回答by brianLikeApple

Many ways to do it, but I found this is the simplest and easiest way to do so.

有很多方法可以做到,但我发现这是最简单和最简单的方法。

Set UITableViewstyle grouped. Then have two sections and each section has only one row. Replace your image in the row.

设置UITableView样式分组。然后有两个部分,每个部分只有一行。在行中替换您的图像。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    //array is your db, here we just need how many they are
    return [array count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //place your image to cell
}
- (float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    //this is the space
    return 50;
}

回答by JRam13

Simply make the content of your cell clearcolor and give yourself padding by adding height. Add the background image (or color) of your cell however you'd like afterwards by inserting a new view or label of shorter height.

只需将单元格的内容设置为 clearcolor 并通过添加高度给自己填充。添加单元格的背景图像(或颜色),但之后您可以通过插入新视图或较短高度的标签来添加。

This way you can maintain a table with multiple sections and multiple rows.

通过这种方式,您可以维护具有多个部分和多行的表格。

回答by saurabh rathod

You can do it in just 3 lines.

您只需 3 行即可完成。

First set gray color of your main view after that follow these lines.

在遵循这些行之后,首先设置主视图的灰色。

cell.separatorInset = UIEdgeInsetsMake(20, 20, 20, 20);
cell.layer.borderWidth = 5;
cell.layer.borderColor = [UIColor whiteColor].CGColor;

回答by Jugal K Balara

1.First you can Increase heightForRowAtIndexPath size

1.首先你可以增加heightForRowAtIndexPath大小

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
  1. Then add custom view on tableViewCell. then set view.frame according padding you want.
  1. 然后在 上添加自定义视图tableViewCell。然后根据你想要的填充设置 view.frame 。

回答by Bavafaali

in swift 5.1 :

在 swift 5.1 中:

   override func numberOfSections(in tableView: UITableView) -> Int {
       return 2
   }
   override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return 1
   }

   override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
       return 15
   }

   override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
       return 30
   }