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

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

问题描述

是否可以保持 contentView.frame 始终相同,而不管 tableView.editing 是什么?我已经尝试覆盖 layoutSubviewswillTransitionToState 但这些选项也失败了.我似乎无法更改 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.

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天全站免登陆