【问题标题】:Swipe to delete not working properly with custom XIB cell滑动以删除自定义 XIB 单元格无法正常工作
【发布时间】:2014-07-21 11:28:01
【问题描述】:

我在 UITableView 中使用从 XIB 加载的单元格的滑动删除功能遇到问题。正如您在下面看到的,当我将单元格向左滑动时,图像会向左移动,但标签不会。所以标签覆盖了删除按钮。您可以在下面找到我已实现的代码:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //remove the deleted object from your data source.
        //If your data source is an NSMutableArray, do this
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"MostKadroCell";

    MostKadroCell *cell = (MostKadroCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];


    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    [cell.contentView setUserInteractionEnabled: NO]; //

    cell.lblKadroName.text = [arrKadroName objectAtIndex:indexPath.row];

[cell.imageView setImage:[UIImage imageNamed:strImage]];
    return cell;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

     UINib *nib = [UINib nibWithNibName:@"MostKadroCell" bundle:nil];
     [self.tableMostKadro registerNib:nib forCellReuseIdentifier:@"MostKadroCell"];
     ....

【问题讨论】:

  • 您可能需要将标签“粘贴”到单元格的左边缘或Interface Builder中的UIImageView
  • 使用NSLayoutConstraint 设置自定义单元格的布局。可以在 IB 中添加,也可以在代码中添加。
  • 虽然我粘贴并使用了布局中的约束,但它不起作用。

标签: ios uitableview


【解决方案1】:

将标签保留在 UITableView 单元格的内容视图下方(内部)。请检查它是否不在内容视图之外。

并添加

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
     return UITableViewCellEditingStyleDelete;
}

视图应该看起来像

【讨论】:

  • 当我将标签放在 UIView 中时,删除按钮会在 UIView 后面。此外,它不像图像视图那样向左滑动。
  • 您能在此处添加 xib 中视图层次结构的屏幕截图吗?
  • 当我按照您的说明修改它时,删除按钮位于内容视图的后面,这意味着带有标签和图像视图的内容视图不会向左滑动。
  • 请添加您的 nib 视图的屏幕截图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多