iPhone UITableView - 删除按钮

2024-05-05

我正在使用“滑动删除”功能UITableView.

问题是我正在使用定制的UITableViewCell这是在每个项目的基础上创建的

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

我需要改变删除按钮的位置(只需将其向左移动 10px 左右),我该如何做呢?

这是我用于创建单元格的现有代码:

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"cellForRowAtIndexPath");
#if USE_CUSTOM_DRAWING
    const NSInteger TOP_LABEL_TAG = 1001;
    const NSInteger BOTTOM_LABEL_TAG = 1002;
    UILabel *topLabel;
    UILabel *bottomLabel;
#endif

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        //
        // Create the cell.
        //
        cell =
        [[[UITableViewCell alloc]
          initWithFrame:CGRectZero
          reuseIdentifier:CellIdentifier]
         autorelease];

#if USE_CUSTOM_DRAWING


        const CGFloat LABEL_HEIGHT = 20;
        UIImage *image = [UIImage imageNamed:@"trans_clock.png"];

        //
        // Create the label for the top row of text
        //
        topLabel =
        [[[UILabel alloc]
          initWithFrame:
          CGRectMake(
                     image.size.width + 2.0 * cell.indentationWidth,
                     0.5 * (aTableView.rowHeight - 2 * LABEL_HEIGHT),
                     aTableView.bounds.size.width -
                     image.size.width - 4.0 * cell.indentationWidth
                     ,
                     LABEL_HEIGHT)]
         autorelease];
        [cell.contentView addSubview:topLabel];

        //
        // Configure the properties for the text that are the same on every row
        //
        topLabel.tag = TOP_LABEL_TAG;
        topLabel.backgroundColor = [UIColor clearColor];
        topLabel.textColor = fontColor;
        topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
        topLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];

        //
        // Create the label for the top row of text
        //
        bottomLabel =
        [[[UILabel alloc]
          initWithFrame:
          CGRectMake(
                     image.size.width + 2.0 * cell.indentationWidth,
                     0.5 * (aTableView.rowHeight - 2 * LABEL_HEIGHT) + LABEL_HEIGHT,
                     aTableView.bounds.size.width -
                     image.size.width - 4.0 * cell.indentationWidth
                     ,
                     LABEL_HEIGHT)]
         autorelease];
        [cell.contentView addSubview:bottomLabel];

        //
        // Configure the properties for the text that are the same on every row
        //
        bottomLabel.tag = BOTTOM_LABEL_TAG;
        bottomLabel.backgroundColor = [UIColor clearColor];
        bottomLabel.textColor = fontColor;
        bottomLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
        bottomLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize] - 2];

        //
        // Create a background image view.
        //
        cell.backgroundView =
        [[[UIImageView alloc] init] autorelease];
        cell.selectedBackgroundView =
        [[[UIImageView alloc] init] autorelease];
#endif
    }

#if USE_CUSTOM_DRAWING
    else
    {
        topLabel = (UILabel *)[cell viewWithTag:TOP_LABEL_TAG];
        bottomLabel = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG];
    }
    topLabel.text  = @"Example Text";
    topLabel.textColor = fontColor;

    bottomLabel.text = @"More Example Text";
    bottomLabel.textColor = fontColor;

    //
    // Set the background and selected background images for the text.
    // Since we will round the corners at the top and bottom of sections, we
    // need to conditionally choose the images based on the row index and the
    // number of rows in the section.
    //
    UIImage *rowBackground;
    UIImage *selectionBackground;
    NSInteger sectionRows = [aTableView numberOfRowsInSection:[indexPath section]];
    NSInteger row = [indexPath row];
    if (row == 0 && row == sectionRows - 1)
    {
        rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"];
        selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];
    }
    else if (row == 0)
    {
        rowBackground = [UIImage imageNamed:@"topRow.png"];
        selectionBackground = [UIImage imageNamed:@"topRowSelected.png"];
    }
    else if (row == sectionRows - 1)
    {
        rowBackground = [UIImage imageNamed:@"bottomRow.png"];
        selectionBackground = [UIImage imageNamed:@"bottomRowSelected.png"];
    }
    else
    {
        rowBackground = [UIImage imageNamed:@"middleRow.png"];
        selectionBackground = [UIImage imageNamed:@"middleRowSelected.png"];
    }
    ((UIImageView *)cell.backgroundView).image = rowBackground;
    ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;

    cell.imageView.image = [UIImage imageNamed:@"Example_Image.png"];
#else
    cell.text = @"Example";
#endif
    return cell;
}

对我来说,解决这个问题的最好方法就是压倒一切-(void)layoutSubviews in MyCell:UITableViewCell

在这里,您不仅可以看到删除按钮自定义位置,还可以重新定位编辑和重新排序控件Edit mode

- (void)layoutSubviews
{
    [super layoutSubviews];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.0f];

    for (UIView *subview in self.subviews) {


        if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) { 
            CGRect newFrame = subview.frame;
            newFrame.origin.x = 200;
            subview.frame = newFrame;
        }
        else if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellEditControl"]) {             
            CGRect newFrame = subview.frame;
            newFrame.origin.x = 100;
            subview.frame = newFrame;
        }
        else if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellReorderControl"]) {             
            CGRect newFrame = subview.frame;
            newFrame.origin.x = 200;
            subview.frame = newFrame;
        }
    }
    [UIView commitAnimations];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iPhone UITableView - 删除按钮 的相关文章

  • 如何将“前往此处的路线”按钮添加到地址簿中?

    谷歌地图应用程序有 到这里的方向 和 从这里出发的方向 例如http static intomobile com wp content uploads 2008 10 iphone 22 os google maps street view
  • iPhone:OpenGL ES:检测您是否点击了屏幕上的对象(立方体)

    我已经问了一个类似的问题 这让我达到了现在的水平 但我真的需要一些帮助 这是我完成一些很酷的事情的最后一件事 在我看来哈哈 我有一个 3D 世界 我可以在其中移动 这个世界里有简单的立方体 使用函数 CGPoint getScreenCoo
  • iPhone——是否可以在 Xcode 调试器中检查 UIView 的框架?

    当调试器在断点处停止时 我在那里找不到任何 UIView 的框架 是否有可能做到这一点 编辑 由于缺乏回应而开始赏金 需要明确的是 我正在寻找一种在不添加额外调试代码的情况下查看框架的方法 另外 如果答案是 不 你不能这样做 赏金将用于解释
  • 如何在 iBooks 上搜索桌面 UI

    我想像 iBooks 中那样显示 tableview 的搜索栏 我如何减少搜索栏的宽度以及如何在没有任何背景颜色的情况下显示它 另外 当页面显示时 如何最初隐藏搜索框 我可以想到两个选择 通过子类化 UITextfield 创建您自己的 U
  • 将 UITableViewDataSource 与具有子视图的自定义单元格结合使用

    在 UITableView 中使用自定义单元格时 我遇到了奇怪的表格重叠 Problem 向下滚动 最后两行上面画有顶部两行 向上滚动 前两行上面画有下面两行 这是 UITableViewDataSource 的代码 func tableV
  • tableView.dequeueReusableCellWithIdentifier() 导致应用程序挂起

    原帖 我们最近将我们的应用程序转换为 Swift 2 0 和 iOS9 我看到的一个奇怪的问题是调用 tableView dequeueReusableCellWithIdentifier 会导致应用程序挂在模拟器中 The code fu
  • iPhone 相当于 Application.DoEvents();

    iPHone 我们使用 MonoTouch 但 Obj C 答案还可以 我的单例域对象需要一段时间才能获取所有数据 因此它在线程中内部运行部分获取数据 我需要通知 UI 域已完成 目前我正在这样做 有没有更好的办法 在 WinForms 中
  • 当我从我转向的视图控制器返回时,为什么我的 UITableView 的格式完全出错了?

    我有一个UITableView使用自定义单元格 其中有一些标签可以动态决定单元格的高度 当我点击一个单元格并转到一个新的视图控制器时 返回后所有单元格的格式完全混乱 我无法弄清楚是什么导致了它 这是细胞通常的样子 我对它们设置了一些非常基本
  • 当应用程序退出活动状态时,MPMovies PlayerViewController 被解雇

    当我将 iPhone 设置为睡眠状态 切换到另一个应用程序等 然后再次返回时 之前的可见内容MPMoviePlayerViewController 提出与presentMoviePlayerViewControllerAnimated 已经
  • UITableview 中的水平和垂直滚动[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 I want to make a lineup for a festival You can see what I want to a
  • iPhone 中的 NSNotification

    我正在将 NSSNotifcation 发送到 iPhone 应用程序中的另一个视图控制器 但它的观察者方法收到两次通知 这可能如何 任何人都可以指导我 我已经使用此代码来发布通知 NSNotificationCenter defaultC
  • 最小的 iOS 蓝牙管理器示例

    我一直在构建一个最小的示例 用于使用 iOS 5 0 中的 BluetoothManager 私有框架来检测附近的蓝牙设备 使用此问题中找到的答案 寻找触手可及的通用蓝牙设备 https stackoverflow com question
  • 在 Objective-C 中比较 2 个字符串

    我写了以下代码 if depSelectedIndice gt 1 comSelectedIndice gt 1 NSLog depart elemet d depSelectedIndice NSLog depart elemet d c
  • 从 UIImagePickerController 相机视图推送 viewController

    我正在开发一款消息应用程序 类似于 WhatsApp 用户可以互相发送文本和图像消息 当用户想要发送图像时 他可以从相机胶卷中选择一张图像 也可以用相机拍摄一张图像 这就是我介绍的方式UIImagePickerController对于这两种
  • 打乱 NSMutableArray 而不重复并显示在 UIButton 中

    在我看来 我有 12 个按钮 一个数组包含 6 个名称 我想在其中打印数组名称UIButton标题 这是我的代码 texts NSMutableArray alloc initWithObjects 1 2 3 4 5 6 nil UIBu
  • 网站在 iPhone 屏幕右侧显示空白区域

    我遇到问题http eiglaw com http eiglaw com iPhone 屏幕右侧显示约 25 像素宽的空白 边框 我在 stackoverflow 上研究了这个问题 这些帖子是相关的 但是当我尝试提供的各种解决方案时 我无法
  • XCode 无法将 iPhone 应用程序部署到 iPhone 3GS

    因此 我构建了我的 iPhone 应用程序 它在模拟器中运行良好 因此我想将其部署到我的 iPhone 上进行最后一轮测试 然后再使用我的分发配置文件进行公开 Beta 测试 我已经这样做过很多次了 从来没有出现过问题 然而 自从上次测试运
  • 如何将 NSDecimal 值转换为 NSInteger 值?

    我遇到一种情况 我得到一个 NSDecimal 并且我需要一个 NSInteger 我确实知道这是一个非常小的值 这是绝对肯定的 它不会大于 100 所以将它转换为 NSInteger 就完全没问题 不会发生溢出 这怎么可能做到呢 NSDe
  • 如何在 iOS 8 中关闭两个 UIViewController?

    我正在使用 Objective C 开发 iPhone 应用程序 由于我需要一次关闭两个 UIViewController 所以我使用下面的代码 self presentingViewController presentingViewCon
  • UIView晃动动画

    我试图在按下按钮时使 UIView 摇动 我正在调整我找到的代码http www cimgf com 2008 02 27 core animation tutorial window shake effect http www cimgf

随机推荐