【问题标题】:iOS: How to move UITableViewCellAccessory Button to the leftiOS:如何将 UITableViewCell 附件按钮向左移动
【发布时间】:2012-06-13 17:08:10
【问题描述】:

我正在尝试开发一个 UITableView,它具有 sectionIndexTitles 并且哪些单元格具有辅助按钮。

问题是索引标题的滚动条减小了单元格的宽度,因此附件按钮的位置很丑......

是否可以将按钮稍微向左移动以使其位于单元格内?

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = @"text";
    }

    return cell;
}

如你所见,我不操纵单元格或类似的东西。

在这里你可以看到结果:

http://i.stack.imgur.com/3AB89.jpg

【问题讨论】:

  • 实际上我操纵了没有额外自定义行的代码(我已经在没有这些的情况下对其进行了测试 -> 相同的结果)

标签: ios ipad uitableview accessorytype


【解决方案1】:

有一种方法可以移动默认的附件视图,但它非常笨拙。因此,当新的 sdk 到达时,它可能会停止工作。

使用风险自负(此代码 sn-p 将任何附件视图向左移动 8 个像素,将其插入所需的 uitableviewcell 子类的 -(void)layoutSubviews 方法):

if (self.accessoryView) {
    r = self.accessoryView.frame;
    r.origin.x -= 8;
    self.accessoryView.frame = r;
} else {
    UIView* defaultAccessoryView = nil;
    for (UIView* subview in self.subviews) {
        if (subview != self.textLabel && 
            subview != self.detailTextLabel && 
            subview != self.backgroundView && 
            subview != self.contentView &&
            subview != self.selectedBackgroundView &&
            subview != self.imageView) {
            defaultAccessoryView = subview;
            break;
        }
    }
    r = defaultAccessoryView.frame;
    r.origin.x -= 8;
    defaultAccessoryView.frame = r;
}

【讨论】:

  • 感谢您的回复!首先:它奏效了!谢谢!查看默认行为(没有这个hack)我注意到在iPhone上,当有一个部分标题滚动条但附件按钮很好地移动到左边时,单元格没有调整大小,但在iPad上却完全相反: 单元格被调整了大小,而附件按钮没有……你知道为什么会这样吗?
  • 这很奇怪。首先,TBH 我根本不推荐该代码。这是一个黑客,可以随时打破。最好的方法是获取您自己的 UITableViewCellAccessory 图像,然后使用 addSubview 将其添加到单元格并使用它调整框架。这是最简单的方法,因为没有内置方法来调整 UITableViewAccessory 的框架。
  • 是的,我试图找到一种解决方案,使这种方式变得不那么“hacky”,但似乎我必须付出一些额外的努力;)感谢您的帮助!
  • 我无法点击tableview,不知道为什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-22
  • 2023-03-23
  • 2023-03-30
  • 1970-01-01
相关资源
最近更新 更多