【问题标题】:How to create padding(spacing) between rows in tablview如何在表格视图中的行之间创建填充(间距)
【发布时间】:2013-07-05 13:35:29
【问题描述】:

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

但这就是发生的事情:

我需要单元格为图像大小,但不同行之间要有间距。我该怎么做?

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    有很多方法,但我发现这是最简单、最简单的方法。

    设置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;
    }
    

    【讨论】:

    • 我的 UITableview 样式已经分组,我没有只显示 2 张图像,我有多少是我存储在数据库中的,你所说的扇区是什么意思?
    • 您的意思是创建静态单元格并使用部分吗?我不能这样做,因为我不知道我需要多少个细胞
    • 对不起,我的错。我的意思是部门而不是部门。我更新了答案。这应该可以解决您的问题。
    • 这可行,但现在我遇到了一个问题。这是我用来在每个单元格内创建图像的代码: GameTypeItem *item = [items objectAtIndex:indexPath.row];现在每个单元格都包含第一张图片(电影)我怎样才能得到我所在的部分的编号?我的意思是插入 indexPath.row 我需要类似 tableView.sections.currentSectionNumber
    • 更改“GameTypeItem *item = [items objectAtIndex:indexPath.row];” to GameTypeItem *item = [items objectAtIndex:indexPath.section];
    【解决方案2】:

    只需 3 行即可完成。

    首先设置你的主视图的灰色颜色,然后按照这些线。

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

    【讨论】:

      【解决方案3】:

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

      这样您就可以维护一个包含多个部分和多行的表格。

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

          在 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
             } 
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-03-06
            • 2011-06-04
            • 2017-12-04
            • 2016-01-05
            • 2019-01-31
            相关资源
            最近更新 更多