在编辑时防止缩进UITableViewCell(contentView) [英] Prevent indentation of UITableViewCell (contentView) while editing

查看:85
本文介绍了在编辑时防止缩进UITableViewCell(contentView)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以保持 contentView.frame 始终相同,而不管 tableView.editing ?我已经尝试重写 layoutSubviews willTransitionToState 但这些选项也失败了。我似乎不能够改变contentView的宽度。或者也许我的方法根本不可能...
也许还有另一种方法来解决这个问题。

Is it possible to keep the contentView.frame always the same, regardless of tableView.editing? I already tried to override layoutSubviews and willTransitionToState but those options fizzled out too. I can't seem to be able to change the width of the contentView. Or maybe my approach ist just plain impossible ... Maybe there is another way to solve this.

我想实现的行为如下:希望始终缩进的 UITableViewCell 的标准 textLabel tableView进入编辑模式。我可能会面临的问题是 detailTextLabel 的行为将必须更正(例如截断文本,如果 textLabel 内容太长)。我不想实现我自己的 UILabel 的原因是因为自定义子视图会大幅降低滚动性能。

The behaviour I want to achieve is the following: I want the standard textLabel of a UITableViewCell to be always indented and not change position when the tableView enters editing mode. The problem I will probably face is that the behaviour of the detailTextLabel will have to be corrected (e.g. truncation of text if textLabelcontent is too long). The reason why I don't want to implement my own UILabelis because a custom subviews will decrease the scrolling performance by a significant amount.

我希望任何人已经在他们的 UITableView 中实现了这样的东西,并可以给我一个解决这个冗长的问题的解决方案。提前感谢!

I hope that anyone already implemented something like this in their UITableViewand could show me a solution to this tedious problem. Thanks in advance!

编辑:我处理的是一个 UITableView p>

I'm dealing with a UITableView in plain and not grouped style.

推荐答案

幸运的是,iOS完全可以保持任何值不变。

Luckily, iOS is perfectly equipped to keep whatever value constant.

这个(丑陋的)代码段将框架固定为 origin.x == 0 的任何值。

This (ugly) piece of code will keep the frame fixed to whatever value has origin.x==0. This is easily adapted to your particular needs.

// put this in your UITableViewCell subclass
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"observed value for kp %@ changed: %@",keyPath,change);
    if ( [keyPath isEqual:@"frame"] && object == self.contentView )
    {
        CGRect newFrame = self.contentView.frame;
        CGRect oldFrame = [[change objectForKey:NSKeyValueChangeOldKey] CGRectValue];
        NSLog(@"frame old: %@  new: %@",NSStringFromCGRect(oldFrame),NSStringFromCGRect(newFrame));

        if ( newFrame.origin.x != 0 ) self.contentView.frame = oldFrame;
    }
}

// add the cell as an observer for frame changes, somewhere in initialization:
[self.contentView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionOld context:nil];

// IMPORTANT: remove the cell as an observer in -dealloc:
[self.contentView removeObserver:self forKeyPath:@"frame"];

这将只允许框架更改为 origin.x == 0 。删除发布版本的 NSLog()行。

This will only allow the frame to change to values with an origin.x==0. Remove the NSLog() lines for Release builds.

我已经测试了几分钟,见到任何副作用。

I have tested this for a few minutes and haven't seen any side effect.

这篇关于在编辑时防止缩进UITableViewCell(contentView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆