share中简单聊天界面的实现<代码>
来源:互联网 发布:手机知乎怎么修改答案 编辑:程序博客网 时间:2024/06/16 06:52
关于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
0 0
- share中简单聊天界面的实现<代码>
- share中简单聊天界面的实现<代码>
- 简单实现带界面的一对一聊天
- 一个简单聊天界面的UI实现
- android中实现简单双方聊天的代码
- NGUI聊天界面简单实现
- 聊天界面关键代码实现
- recycleview多item布局的实现(简单聊天界面)
- 编写简单的聊天界面
- iOS 类似微信,QQ聊天界面的气泡聊天简单实现Demo
- iOS 类似微信,QQ聊天界面的气泡聊天简单实现Demo
- 一个简单的聊天代码
- 简单的聊天界面demo FaceChatDemo
- 编写一个简单的聊天界面
- 通过代码自定义cell——实现qq聊天界面(Version 2 实现聊天内容的背景)
- 非常简陋的聊天界面代码
- Android(安卓)一个简单的聊天界面的实现(eclipse实现)
- 简单聊天界面
- 欧几里得算法 最大公约数(Java)
- 【C语言提高02】数据类型本质
- 爬爬爬之路:UI(十二) 单例 UITabBarController 高级Block的应用
- 自学objective-c,靠什么坚持?
- 栈增长方向与大端/小端问题
- share中简单聊天界面的实现<代码>
- uva 12563——Jin Ge Jin Qu hao
- 地理信息系统基础算法(三)判断矩形是否包含点
- source not found. debug
- 【OpenCV学习笔记】1.2图像的基本操作
- MFC----定制程序外观
- 闲话Zynq UltraScale+ MPSoC (连载3)——启动加载
- 回家了,英语学习也要抓紧了!
- 1026. Table Tennis (30)