【问题标题】:UITableViewRowAction custom style?UITableViewRowAction 自定义样式?
【发布时间】:2014-11-13 10:57:37
【问题描述】:

我想编辑我的表的UITableViewRowAction。我的单元格有圆边和边框,我想编辑RowAction 的样式以匹配我的单元格的样式。

我了解目前只有backgroundColor,标题和样式(默认、破坏性、正常...)可以更改(link)。但是,我觉得这有点太局限了?也许我可以继承UITableViewRowAction 来创建自己的外观和感觉?任何建议将不胜感激。

很遗憾,我无法发布示例图片,因为我还没有 10 个代表(哈哈)。但是我认为这个问题很简单,无需视觉表示即可理解。

【问题讨论】:

  • 您的要求是为删除的按钮添加边框
  • moreAction.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"temp.png"]] 可以添加需要的图片来获取
  • 感谢您的快速回复。我已经测试过这个解决方案并且它确实有效,但是删除按钮消失(一旦点击)的动画看起来有点奇怪,因为图像的圆形边缘实际上并没有移动,而是随着直边向上滚动而消失以显示下面的空白处。我意识到这可能是目前最好的解决方案,因为 UITableViewRowAction 的功能有限;但在我看来,它的 API 似乎有点草率!
  • 感谢您的测试。我会发布解决方案以帮助其他人。

标签: ios objective-c uitableview


【解决方案1】:

正如您所说,您可以更改的唯一操作是:

  1. 背景颜色
  2. 风格(破坏性(红色背景色,...)
  3. 标题

为了改变 UITableViewRowAction 的样式,可能的解决方案是使用“patternimage”添加图像。

这是解决方案。我并不是说这是确切的解决方案,因为现在我们无法访问“UITableViewRowAction”的所有属性

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *moreAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"test" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

    // show UIActionSheet
}];
float wwidth =[moreAction.title sizeWithAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:15.0]}].width;
wwidth = wwidth+<55 change this value for exact width>;
moreAction.backgroundColor = [[UIColor alloc] initWithPatternImage:[self imageWithImage:[UIImage imageNamed:@"temp.jpg"] scaledToSize:CGSizeMake(wwidth, tableView.rowHeight)]];

UITableViewRowAction *flagAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Flag" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
    // flag the row
}];
flagAction.backgroundColor = [UIColor yellowColor];

return @[moreAction, flagAction];
}

注意:使用高分辨率图像,以便它适用于

iPhone 6 Plus   736x414 points    5.5"
iPhone 6        667x375 points    4.7"
iPhone 5        568x320 points    4.0"
iPhone 4        480x320 points     3.5"

此链接上有用的帖子http://pablin.org/

【讨论】:

  • 我组装了一个 CocoaPod,它会尝试尽可能优雅地解决这个问题:github.com/benguild/BGTableViewRowActionWithImage
  • @BenGuild 谢谢!
  • [self imageWithImage:[UIImage imageNamed:@"temp.jpg"] scaledToSize:CGSizeMake(wwidth, tableView.rowHeight)] 这是做什么的?
【解决方案2】:

你可以试试这个: Custom UITableViewCell 支持单元格拖动手势的自定义 UITableViewCell 以显示按钮或其他视图的自定义菜单。它支持:

  • 完全可定制的 UIView 用作自定义按钮的画布和 意见;
  • 自动布局;
  • 故事板;
  • 易于细胞重复使用;
  • 高亮/取消高亮单元格;
  • 选择/取消选择单元格;
  • 正常和编辑模式;
  • 附件视图;
  • 兼容 iOS 7 和 8。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 2017-02-10
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多