share中简单聊天界面的实现<代码>
2016-07-27 20:03
441 查看
share中简单聊天界面的实现<代码>
主要是用UITableView来实现,先用要发送的消息自适应获得尺寸,在cell上加聊天内容。#import "chatViewController.h" @interface chatViewController ()<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate> { UIView *_textView; //最底下的输入视图 UITextField *_textField; //输入框 NSMutableArray *_array; //存储发过的消息 UITableView *_tableView; //消息界面 } @end @implementation chatViewController - (void)viewDidLoad { [super viewDidLoad]; _array=[[NSMutableArray alloc]initWithCapacity:0]; _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 633) style:UITableViewStylePlain]; _tableView.delegate=self; _tableView.dataSource=self; [self.view addSubview:_tableView]; _textView=[[UIView alloc]initWithFrame:CGRectMake(0, 600, 375, 44)]; _textView.backgroundColor=[UIColor redColor]; [self.view addSubview:_textView]; _textField=[[UITextField alloc]initWithFrame:CGRectMake(10, 5, 310, 34)]; _textField.borderStyle=UITextBorderStyleRoundedRect; _textField.delegate=self; [_textView addSubview:_textField]; UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame=CGRectMake(320, 5, 55, 34); [button setTitle:@"发送" forState:UIControlStateNormal]; [button addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside]; [_textView addSubview:button]; } -(void)sendMessage{ NSDictionary *dictionary=@{NSFontAttributeName:[UIFont systemFontOfSize:18]}; CGSize size=[_textField.text boundingRectWithSize:CGSizeMake(305, 10000) options:NSStringDrawingTruncatesLastVisibleLine attributes:dictionary context:nil].size; //自适应尺寸 UIView *view=[[UIView alloc]initWithFrame:CGRectMake(255-size.width, 0, size.width+90, size.height+60)]; view.tag=121; //view是cell上加的视图 UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 15, size.width+40, size.height+30)]; imageView.image=[UIImage imageNamed:@"chat1"]; [view addSubview:imageView]; //气泡的图片加到view UILabel *lable=[[UILabel alloc]initWithFrame:CGRectMake(20, 15, size.width, size.height)]; lable.text=_textField.text; lable.font=[UIFont systemFontOfSize:18]; lable.numberOfLines=0; [imageView addSubview:lable]; //聊天内容加到气泡上 UIImageView *headImageView=[[UIImageView alloc]initWithFrame:CGRectMake(size.width+40, 10, 60, 60)]; headImageView.image=[UIImage imageNamed:@"sixin_img1"]; [view addSubview:headImageView]; //用户头像 [_array addObject:view]; //将view加到数组中 [_tableView reloadData]; //刷新数据 [_tableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionBottom animated:YES]; //滑动viewController到最后一行 _textField.text=@""; //textField内容清空 } //编辑开始时开启动画上移textView和tableView,给键盘留出空间 -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.2]; _textView.frame=CGRectMake(0, 340, 375, 44); _tableView.frame=CGRectMake(0, 0, 375, 500); [UIView commitAnimations]; return YES; } //编辑完成时开启动画下移textView和tableView -(BOOL)textFieldShouldReturn:(UITextField *)textField{ [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.2]; _textView.frame=CGRectMake(0, 623, 375, 44); _tableView.frame=CGRectMake(0, 0, 375, 633); [UIView commitAnimations]; [textField resignFirstResponder]; return YES; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _array.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellIdentifier=@"cell"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; cell.selectionStyle=UITableViewCellSelectionStyleNone; } UIView *cellView=[cell.contentView viewWithTag:121]; [cellView removeFromSuperview]; UIView *view=[_array objectAtIndex:indexPath.row]; [cell.contentView addSubview:view]; //在cell上加聊天内容 return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ UIView *view=[_array objectAtIndex:indexPath.row]; return view.frame.size.height; //返回cell行高 } @end
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- [原创]java局域网聊天系统
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- winform异型不规则界面设计的实现方法
- Android聊天工具基于socket实现
- PHP+swoole实现简单多人在线聊天群发
- c#实现多线程局域网聊天系统
- 在ASP.NET 2.0中操作数据之二十:定制数据修改界面
- nw.js实现类似微信的聊天软件
- javascript Discuz代码中的msn聊天小功能
- Android中设置只有程序第一次运行才显示的界面实现思路
- hta 实现的五子棋界面
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法