简体   繁体   English

如何在表视图单元格ios中垂直对齐文本视图文本?

[英]How to align text view text vertically in a table view cell ios?

I tried to align the text of an UITextView. 我试图对齐UITextView的文本。 My text view is in a cell. 我的文本视图在一个单元格中。 Using this link by stackoverflow I tried to align the text. 通过stackoverflow使用此链接,我尝试对齐文本。 I did it in TableViewCell class like following, 我是在TableViewCell类中做到的,如下所示,

- (void)awakeFromNib {
    [self.postedText addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    UITextView *txtview = object;
    CGFloat topoffset = ([txtview bounds].size.height - [txtview contentSize].height * [txtview zoomScale])/2.0;
    topoffset = ( topoffset < 0.0 ? 0.0 : topoffset );
    txtview.contentOffset = (CGPoint){.x = 0, .y = -topoffset};
}

When I'm running this, I got the following error. 当我运行此程序时,出现以下错误。

observeValueForKeyPath:ofObject:change:context: message was received but not handled. watchValueForKeyPath:ofObject:change:context:消息已收到但未处理。 Key path: contentSize Observed object: ; 关键路径:contentSize观察到的对象:; layer = ; 层=; contentOffset: {0, 0}; contentOffset:{0,0}; contentSize: {256, 204}> Change: { kind = 1; contentSize:{256,204}>更改:{kind = 1; new = "NSSize: {256, 204}"; new =“ NSSize:{256,204}”; } Context: 0x0' *** First throw call stack: (0x1821a02d8 0x1939740e4 0x1821a0218 0x183075d44 0x182fc95c4 0x182fc90e4 0x182fb288c 0x187315944 0x186c8f024 0x186bdd760 0x186525e1c 0x186520884 0x186520728 0x18651febc 0x18651fc3c 0x186c63f90 0x186c63ef4 0x1821582a4 0x182155230 0x182155560 0x1820812d4 0x18b89f6fc 0x186c46fac 0x100060414 0x193ff2a08) libc++abi.dylib: terminating with uncaught exception of type NSException }上下文:为0x0' ***第一掷调用堆栈:(0x1821a02d8 0x1939740e4 0x1821a0218 0x183075d44 0x182fc95c4 0x182fc90e4 0x182fb288c 0x187315944 0x186c8f024 0x186bdd760 0x186525e1c 0x186520884 0x186520728 0x18651febc 0x18651fc3c 0x186c63f90 0x186c63ef4 0x1821582a4 0x182155230 0x182155560 0x1820812d4 0x18b89f6fc 0x186c46fac 0x100060414 0x193ff2a08)的libc ++ abi.dylib:与未捕获的终止NSException类型的异常

How may I fix this? 我该如何解决?

How do you know that self will persist? 你怎么知道self会坚持下去? This is, I take it, a table cell; 我认为这是一个表格单元格; they come and go. 他们来来去去。 Making the table cell the observer in KVO is very dangerous (as you've just discovered). 使表单元成为KVO中的观察者非常危险(正如您刚刚发现的那样)。

In general this approach is not going to be tenable, because the observer needs both to persist beyond the life of the observed and it needs to unregister itself with the observed before the observed goes out of existence. 一般而言,这种方法将不会成立,因为观察者需要两者都坚持到观察者的生命周期之外,并且需要在观察者不复存在之前向观察者注销自己的身份。 You're not going to be able to do either of those things. 您将无法做任何一件事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM