在编辑时防止缩进UITableViewCell(contentView) [英] Prevent indentation of UITableViewCell (contentView) while editing
问题描述
是否可以保持 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 textLabel
content is too long). The reason why I don't want to implement my own UILabel
is 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 UITableView
and 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屋!