【问题标题】:How to add an image in UITableView's content view while we reodering cells?重新排序单元格时如何在 UITableViewcell 内容视图中添加图像?
【发布时间】:2012-11-06 12:59:44
【问题描述】:

我有一个表格视图,可以在其中提供重新编码单元格的选项。但是,我不想在用户重新排列单元格时显示删除图标。所以我确实喜欢this 删除删除按钮。

在正常模式下..

编辑后模式(即不可删除但可重新排列的模式)..

在这里,发生了两件事。一个是每个单元格的附件视图都有一个图像(表示重新排列)。第二件事是,所有字符串都从左侧屏幕移动了一些点。这个间隙实际上是针对删除符号的。由于我隐藏了删除按钮,所以出现了一个空白区域。

好的,问题来了。我现在想在每个单元格的内容视图中添加一些带有图像的自定义按钮。

所以,我在tableView: cellForRowAtIndexPath:中添加了以下代码

if (isEditable)
{
    selectionButton = [UIButton buttonWithType:UIButtonTypeCustom];

    UIImage *img = [UIImage imageNamed:@"blue_button.png"];

    selectionButton.frame = CGRectMake(-25, 10, img.size.width, img.size.height);

    [selectionButton setBackgroundImage:img forState:UIControlStateNormal];

    [selectionButton addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];

    [cell.contentView addSubview:selectionButton];
}

蓝色按钮为 24*24 像素。

我使用 -25 作为“x”值的原因:

如果我给出一个正值或零,按钮将与单元格文本重叠。下图表示x值为0时。

但是对于 x=-25,

我需要什么:

由于我添加了带有负 x 值的按钮,因此整个图像宽度(24 像素)将隐藏在 x 值(25 像素)中。所以按钮操作buttonClickAction: 没有被调用。我想在用户点击图像时更改图像(在选定/未选定模式之间切换)。

有什么方法可以调用按钮的action方法吗?还是我应该创建一个自定义单元格?

只是困惑..

【问题讨论】:

    标签: objective-c uitableview cocoa-touch uibutton


    【解决方案1】:

    将此添加到您的表视图控制器

    - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
                return NO;
    }
    

    它将删除空白空间。

    【讨论】:

      猜你喜欢
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多