ios - 滑动删除按钮位于自定义 UITableViewCell 文本下方

标签 ios objective-c xcode uitableview

我在使用自定义 UITableViewCell 滑动删除按钮时遇到问题。当我滑动它时,删除按钮位于单元格中的文本下方。您可以在下面看到我正在使用的图像和代码。

注意:我在xib中绘制tableviewcell。

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
    }
}

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

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

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HareketCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];

    [cell.lblDakika setText:[NSString stringWithFormat:@"%@%@", [[arrProgramim objectAtIndex:indexPath.row]valueForKey:@"dakika"], @" dk"]];

    [cell.imgThumbnail setImage:[UIImage imageNamed:@"most_harita_icon.png"]];

    return cell;
}

enter image description here

最佳答案

那是因为您没有将 UILabel 添加为 UITableViewCellcontentView 的 subview ,而是将其添加为 UITableViewCell 的 subview UITableViewCell 本身。

当您想要子类化 UITableViewCell 并向其添加 subview 时,您应该始终将它们添加到单元格的 contentView 中。

这是来自 Apple 的文档 UITableViewCell :

If you want to go beyond the predefined styles, you can add subviews to the contentView property of the cell.

如果您这样做,iOS 会自动调整 contentView 中的所有内容以显示删除按钮并且没有任何重叠。

更新:

从下面的评论来看,单元格是在单独的 .xib 上创建的,因此标签已经是单元格 contentView 的 subview

问题是在 -tableView:cellForRowAtIndexPath: 中加载 NIB。您必须事先在 TableView 上注册 NIB,例如,在 -viewDidLoad 中。像这样:

- (void)viewDidLoad {
    // Assuming the xib is named HareketCell.xib
    UINib *nib = [UINib nibWithNibName:@"HareketCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"HareketCell"];
}

现在,为此更改 -tableView:cellForRowAtIndexPath::

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

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

    [cell.lblDakika setText:[NSString stringWithFormat:@"%@%@", [[arrProgramim objectAtIndex:indexPath.row]valueForKey:@"dakika"], @" dk"]];

    [cell.imgThumbnail setImage:[UIImage imageNamed:@"most_harita_icon.png"]];

    return cell;
}

你看,-dequeueReusableCellWithIdentifier 已经为你自动加载了 NIB,如果你事先注册的话。

关于ios - 滑动删除按钮位于自定义 UITableViewCell 文本下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24839828/

相关文章:

objective-c - UICollectionView 上的 UISwipeGestureRecognizer 不起作用

ios - __attribute __((constructor))对于iOS编译成什么?如何在手写的asm中进行?

Objective-C:循环协议(protocol)要求

iphone - ResignFirstResponder 不工作

iphone - xcode 4.2 ios5 协同设计失败,退出代码 1

xcode - SwiftUI如何在View在初始化程序中需要@Binding时实例化PreviewProvider

ios - 将 iPhone 应用程序分发给测试人员?

iphone - slider 在 Iphone SDK 的 TableView 中重新加载

javascript - Mobile Safari 视差滚动不起作用

ios - 在 iOS 中调整 UIImageView 的大小