简单聊天界面
来源:互联网 发布:密码框键入数据是 编辑:程序博客网 时间:2024/06/02 06:38
1.在Main.storyboard中创建控制器和控件,添加约束,设置TextField 如图:
2.为TableView在控制器上添加delegate、datasource,为textField添加delegate
3.在ViewController.m中添加代码
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>@property (weak, nonatomic) IBOutlet UITableView *tableView;@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutConstraint;//UIView的底部约束,随着键盘弹出或者收缩而改变@property (nonatomic,strong) NSMutableArray *msgArray;//保存输入的字符串@end
@implementation ViewController
//对可变数组进行懒加载-(NSMutableArray *)msgArray{ if (!_msgArray) { _msgArray = [NSMutableArray array]; } return _msgArray;}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.
//添加通知,监控键盘的变化 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillChangeFrameNotification object:nil];}-(void)keyboardChange:(NSNotification *)notification{ NSLog(@"%@",notification.userInfo); CGRect keyFrame = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];//获取键盘的Frame CGFloat keyFrameY = keyFrame.origin.y;//获取键盘的坐标Y self.layoutConstraint.constant = [UIScreen mainScreen].bounds.size.height - keyFrameY;//UIView底部的约束}
#param mark - UITextfieldDelegate-(BOOL)textFieldShouldReturn:(UITextField *)textField{ [self.msgArray addObject:textField.text]; //发送之后,使输入框内容为空 textField.text = nil; //加载表视图 [self.tableView reloadData]; NSIndexPath *index = [NSIndexPath indexPathForRow:(self.msgArray.count-1) inSection:0]; //始终滚动出最新一行消息 [self.tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionBottom animated:YES]; return YES;}-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//滚动表视图时,键盘收起
[self.view endEditing:YES ];}#param mark - UITableViewDataSource-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.msgArray.count;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MsgCell" forIndexPath:indexPath]; cell.textLabel.text = [self.msgArray objectAtIndex:indexPath.row]; return cell;}
0 0
- 简单聊天界面
- NGUI聊天界面简单实现
- NGUI制作简单聊天界面
- 编写简单的聊天界面
- 简单的聊天界面demo FaceChatDemo
- 简单实现带界面的一对一聊天
- 一个简单聊天界面的UI实现
- 编写一个简单的聊天界面
- 聊天界面
- 聊天界面
- 聊天界面
- 聊天界面
- 聊天界面
- 聊天界面
- 聊天界面
- 聊天界面
- 聊天界面
- 聊天界面
- 打字小游戏
- JSTL函数标签库的fn标签
- [剑指Offer]面试题35:第一个只出现一次的字符
- xml文件报错找不到资源(字符串、变量)解决方法(尚未解决,待续)
- iOS 推送,证书申请,本地推送
- 简单聊天界面
- gcc与make
- 3.C#多线程补充(委托Delegate)
- HIVE的简介
- Java程序StringBuilder的效率,验证字符串操作和字符串生成器的操作效率
- 一点一滴分析LinkIt™ Smart 7688 编译环境
- Leetcode #17 Letter Combinations of a Phone Number Z9键盘字母组合解题小节
- Windows上已经删除的文件并没有消失并且再次删除却提示无法找到该项目的解决办法
- ASMC(异步静态存储控制器)