您的位置:首页 > 编程语言

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息