ios - 自定义 UITableViewCell 的 UI 问题

标签 ios objective-c uitableview customization

我有一个定制的UITableViewCell .这些有以下问题:

  • 删除按钮太靠右了,与我的背景重叠。我想设置一个插图,让它向左移动一点,然后看起来很棒。

  • Delete button overlapping

    蓝色突出显示的状态跨越整个屏幕宽度,我希望它只是设置在我的弯曲单元格背景的范围内。

    Highlighted state

    有没有办法解决这两个问题?谢谢。

    编辑:我的代码如下设置背景(只是一个自定义方法):
    - (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSInteger rowCount = [self tableView:[self tableView] numberOfRowsInSection:0];
        NSInteger rowIndex = indexPath.row;
        UIImage *background = nil;
    
        if (rowIndex == 0) {
            background = [UIImage imageNamed:@"cell_top.png"];
        } else if (rowIndex == rowCount - 1) {
            background = [UIImage imageNamed:@"cell_bottom.png"];
        } else {
            background = [UIImage imageNamed:@"cell_middle.png"];
        }
    
        return background;
    }
    

    然后我在 reloadTable 中调用此方法void 方法,它只获取每个单元格并在行更新后对其进行更新(添加或删除一行,因为表格有 3 个不同的图像)。在我的 fetched results Controller 从 Core Data 获取我的所有项目并第一次加载表之后,我也会调用它。

    编辑 2:这是 reloadTable 的代码方法:
    - (void)refreshTable
    {
        for (NSInteger j = 0; j < [self.tableView numberOfSections]; ++j)
        {
            for (NSInteger i = 0; i < [self.tableView numberOfRowsInSection:j]; ++i)
            {
                UIImage *background = [self cellBackgroundForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]];
    
                UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
                cellBackgroundView.image = background;
                UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]];
                cell.backgroundView = cellBackgroundView;
            }
        }
    }
    

    问题是:我不知道如何设置表格 View 或表格 View 单元格的边距或宽度。我需要子类化一些东西吗?一般是怎么做的?

    最佳答案

    问题是您的表格 View 框架宽度,所选单元格的蓝色将填满整个单元格,因此您应该减小表格 View 的宽度,以便它有一些 margins这样delete按钮也会出现在左边一点。但是当您选择它们​​时,第一个和最后一个单元格仍然会有问题,因为它们具有圆角但蓝色选择不会有圆角所以我建议您应该为选定状态创建一个 .png 图像(一个第一个单元格,一个用于中间单元格,一个用于底部单元格)

    编辑

    在 iOS 中,margins 的概念不存在,我只是使用该术语,因为我认为它会更容易理解,但在这里你必须做些什么才能在表格 View 的左右显示一点空间:

    yourTableView.frame = CGRectMake(leftMarginSpace,
                                     0,
                                     self.view.frame.size.width 
                                         - leftMarginSpace 
                                         - rightMarginSpace, 
                                     self.view.frame.size.height);
    

    因此,如果您将 TableView 添加到 View Controller 的 View 中,那么 TableView 将位于 leftMarginSpace距左侧的距离,位于 View Controller 的顶部(0 y 位置),位于 rightMarginSpace与右侧的距离并将具有 View Controller 的高度。

    如果您将 TableView 作为 subview 添加到另一个 View (不是 View Controller 的 View ),那么您必须像这样更改框架:
    yourTableView.frame = CGRectMake(leftMarginSpace,
                                         0,
                                         parentView.frame.size.width 
                                             - leftMarginSpace 
                                             - rightMarginSpace, 
                                         parentView.frame.size.height);
    

    我还注意到你想创建一个分组的表格 View 样式,所以你应该检查 grouped table view .

    关于ios - 自定义 UITableViewCell 的 UI 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16597729/

    相关文章:

    swift - 从另一个 TableView 插入行

    ios - 从 Air for iOs 应用程序发送到第二个应用程序的链接

    ios AVAudioPlayer 在后台响起,直到其他东西将其关闭?

    ios - 从数组中获取值

    objective-c - 对文件的一些操作

    ios - 从 NSDirectory 中删除多个文件

    ios - Storyboard UITableViewController 启用屏幕底部的工具栏

    ios - ComponentKit 和 FLUX 架构 - 避免不必要的 UI 重新渲染

    ios - 在 UITableView 中的 UITableViewCells 之间切换

    ios - 从核心数据中删除图像不起作用