UITableView cell嵌套textView
来源:互联网 发布:淘宝正品aj篮球鞋店铺 编辑:程序博客网 时间:2024/06/07 16:05
想必大家会遇到cell里添加textView输入框的需求,这遇见两个问题: 自动计算输入框的高度,以及键盘弹起不能遮盖输入框。此次项目遇见这个问题在此和大家探讨一下。(在此使用YYText)
1.注册键盘通知
//注册通知,监听键盘弹起事件
[[NSNotificationCenterdefaultCenter]addObserver:self
selector:@selector(handleKeyboardDidShow:)
name:UIKeyboardWillShowNotification
object:nil];
//注册通知,监听键盘消失事件
[[NSNotificationCenterdefaultCenter]addObserver:self
selector:@selector(handleKeyboardDidHidden)
name:UIKeyboardWillHideNotification
object:nil];
-(void)textViewDidChange:(YYTextView *)textView{
_newsMainModel.value = textView.text;
CGSize constraintSize =CGSizeMake(kScreenWidth -142,MAXFLOAT);
//计算高度
CGSize size = [textViewsizeThatFits:constraintSize];
if (size.height <=30) {
self.contentHeight =30;
}elseif(size.height <80){
self.contentHeight = size.height;
}
}
- (void)setContentHeight:(float)contentHeight{
if (_contentHeight != contentHeight) {
_contentHeight = contentHeight;
_textView.height = contentHeight;
//把高度赋值给model
_newsMainModel.inputHeight = contentHeight;
NewMainListController *mainListCon = (NewMainListController *)self.viewController;
//刷新cell的高度(在此不能用reloadData,会造成输入失去焦点,你可以试一下)
[mainListCon.mainTabbeginUpdates];
[mainListCon.mainTabendUpdates];
}
}
3.回调获取焦点的输入框写在cell里)
-(void)textViewDidBeginEditing:(YYTextView *)textView{
if (_rowBlock) {
_rowBlock(_indexPaths);
}
}
- (void)handleKeyboardDidShow:(NSNotification*)paramNotification{
//获取键盘高度
NSValue *keyboardRectAsObject=[[paramNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect;
[keyboardRectAsObject getValue:&keyboardRect];
_keyBordHeightt = keyboardRect.size.height;
//获取当前cell在tableView的所处位置
float contentY = [_mainTab rectForRowAtIndexPath:_indexPaths].origin.y;
if (contentY - slidDistance +_keyBordHeight + 80 > kScreenHeight - 64) {
if (contentY - slidDistance + 50 >= kScreenHeight - 64) {
//判断这一页的最后一个输入框,避免有空白
[UIView animateWithDuration:.3 animations:^{
_mainTab.top = -_keyBordHeight;
}];
}else{
[UIView animateWithDuration:.3 animations:^{
_mainTab.top = -(contentY - slidDistance+_keyBordHeight - kScreenHeight + 64 + 80);
}];
}
}
}
- (void)handleKeyboardDidHidden{
[UIView animateWithDuration:.3 animations:^{
_mainTab.top = 0;
}];
}
//删除通知
-(void)dealloc{
[[NSNotificationCenterdefaultCenter]removeObserver:self];
}
- UITableView cell嵌套textView
- Cell嵌套UITableView自动布局
- UITableView Cell
- UITableView Cell
- UItableview上自定义cell,带有textview,控制uitableview滚动到响应位置,不被键盘挡住
- cell中嵌套cell
- iOS开发 UITableView中cell嵌套UITextField(UITextView)引起的复用问题
- 滑动删除uitableview cell
- UItableView cell 自适应高度
- UITableView之cell复用
- UITableView的Cell排序
- UITableView自定义Cell
- UITableView 数据展示 cell
- UITableView cell的重用
- UITableView选中cell
- ios UITableview自定义cell
- <四>UITableView 自定义cell
- UITableView点击展开cell
- 【如何快速的开发一个完整的iOS直播app】(原理篇)
- 第五章 队列
- 吐血React-native之常见问题集合
- os.clock()获取lua代码运行时间
- Algorithm Gossip:三色旗问题
- UITableView cell嵌套textView
- Android学习笔记(1)------简易记事本
- 关于caffe innerproduct层的理解
- F1V3.0-7 Spring Cloud 介绍
- 理解MySQL——复制(Replication)
- 8168 VAG 显示LOGO 以及视频输出
- 低仿知乎ViewPager三层嵌套页面布局
- 让VS2013 MFC程序兼容windows xp系统
- Qt中的mysql数据库