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 - 删除按钮 的相关文章

  • iOS 9 中可滑动的表格视图单元格

    我希望我的表格列表具有像 iOS 8 中那样的可滑动菜单 首次在 iOS 7 中引入 我找到了清晰的 Ray Wenderlich 指南 http www raywenderlich com 62435 make swipeable tab
  • 如何让 Chrome Cast 在 iOS 后台运行?

    我有一个简单的问题 当您进入 iPhone 的主屏幕并且不退出应用程序时 chrome Cast 设备会停止在屏幕上播放视频 当应用程序在后台运行时 我如何才能保持播放状态 如果您有一个视频应用程序并且它在投射设备中运行 您可能需要以下行为
  • 将UIWebView显示的PDF保存到本地

    我有一个UIViewController与UIWebView显示一个 pdf 文件 具体取决于之前单击的行UITableView 现在我想添加一个按钮 供用户在本地保存此 pdf 文件以供离线使用 然后还有第二个UITableView它应该
  • 使 html svg 对象也成为可点击的链接(在 iPhone 上)

    这个问题与使 html svg 对象也成为可点击的链接 https stackoverflow com q 11374059 4825796 但给出的答案似乎不适用于 iPhone ios 9 3 safari 和 chrome 浏览器 我
  • 如何在iPhone中将字节数组转换为base64字符串?

    我有一段vb代码 我需要将字节数组转换为 Base 64 字符串 下面是vb代码 如果 arrLicence Count gt 0 那么 LicenseBytes CType Array CreateInstance GetType Byt
  • xcode 6 资产目录 iPhone 6

    这个问题还没有得到解答 使用资产目录时 特定于设备 非通用 选项为 1x 2x r 2x 3x 1x 是不必要的 因为它不是视网膜 2x 是 ip4 的最佳选择 分辨率为 640x960 r 2x 适合 ip5 分辨率为 640x1136
  • sizeToFit 运行异常

    我有一段代码 每次发生后端数据库更改时都会执行 本质上我在父视图中有一个标签 标签由许多状态消息之一更新 每个状态消息位于不同的行上 并以换行符 n 结尾 每条状态消息只能在一行上 并且不能过多 我遇到的问题是 当视图首次重新加载时 一切正
  • iphone - 如何读取麦克风输入?

    如何从麦克风读取声音数据 我不想录制然后访问数据 我希望能够实时访问声音输入 您可以将麦克风中的声音 PCM 样本 录制到非常短的内存缓冲区 持续时间短至几毫秒 这与使用 iOS API 一样接近 实时 查看 aurioTouch 示例 了
  • 当表格为空时 iPhone UITableView 图像背景

    我想当我的 UITableView 为空时显示图像背景 目前 我尝试将 UIImageView 添加到包含表格的视图控制器 但 XCode 不允许这样做 有没有好的方法呢 您可以在表视图顶部添加图像视图或更改表视图的背景视图 Check i
  • 增量后清除推送通知徽章

    我正在研究 iPhone 中的推送通知 当我收到推送通知时 它在我的应用程序图标上显示 1 下次显示 2 3 4 如果我打开应用程序 它是 0 下次它应该是 1 2 3 4 但它显示最后一个数字和 1 我想在打开应用程序后重置推送通知徽章
  • 如何使用 MonoTouch 在 UIKeyboard 之上添加 UIToolbar?

    我按照 Obj C 中的示例进行操作定制 iPhone 键盘 https stackoverflow com questions 1610542 custom iphone keyboard 1612549 1612549但是 为了在 Wi
  • 应用程序仅启用纵向,但 UIImagePickerController 在 iOS6 中旋转

    请注意 下面的答案 不适用于 iOS6 所以我仍然需要答案 我的应用程序仅启用纵向模式 但是 如果我将 UIImagePickerController 作为子视图嵌入其中 并旋转设备 则顶部和底部栏将保持在同一位置 但 UIImagePic
  • AGVTool new-version 和 What-version 不对应

    当我做 agvtool new version all 99 它更新我的 Info plist 文件 但是 如果我这样做 agvtool what version or agvtool next version 我收到此错误 There d
  • iOS 开发:如何强制 UIWebView 加载 Facebook 的非移动版本?

    我正在深入研究 iOS 开发 当我尝试在 UIWebView 中加载特定的 Facebook 粉丝页面时 它会加载该网站的移动版本 该版本仅加载粉丝页面的墙 而不是我需要加载的特定选项卡 在我的应用程序的 iPad 版本中 UIWebVie
  • 如何正确释放附加 C 数组中的内存?

    我只是想弄清楚为什么下面的代码会泄漏内存 并且我有一种有趣的感觉 我没有正确释放数组内存 这是一个更广泛的 Objective C 应用程序中的 C 函数 我不是 C 语言的本地人 我尝试过在数组上使用 free 但有一种感觉这不是全部故事
  • 如何在 Objective-C 中解析包含 XML 的 NSString?

    在我的 iPhone 应用程序中 我有以下 NSString NSString myxml
  • 使用动画过滤 UITableViewCells - iPhone 开发

    这看起来很简单 但到目前为止我还无法找到解决方案 基本上我有一个带有两个选项的分段控件 第一个是默认值 加载时自动显示 选择后会在表视图中显示所有行 第二个是限制显示行的过滤器 这与 iPhone 电话应用程序的 最近 选项卡上使用的设置完
  • 为什么在 iPhone 应用程序中调用 glMatrixMode(GL_PROJECTION) 会给我 EXC_BAD_ACCESS ?

    我有一个 iPhone 应用程序 我在应用程序 DidFinishLaunching 中调用这三个函数 glMatrixMode GL PROJECTION glOrthof 0 rect size width 0 rect size he
  • 如何比双击更快地识别单击?

    我有一个UITableView与我添加单击的行and双击手势 let doubleTap UITapGestureRecognizer target self action doubleTap doubleTap numberOfTapsR
  • cellForRowAtIndexPath:未调用

    我的应用程序有两种状态 已登录和未登录 并且我有以下架构 大大简化 ViewController A 包含一个搜索框和一个表视图 ViewController B 用于登录应用程序 流程如下 用户未登录 A 被压入堆栈 在viewWillA

随机推荐