#import "AppDelegate.h"
.h
//创建页面视图
ViewController *vc = [[ViewController alloc]init];
//创建导航器对象
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
//修改窗口根视图
self.window.rootViewController = nav;
#import "ViewController.h"
.h
#import "Message.h"
#import "MyTableViewCell.h"
<UITableViewDataSource,UITableViewDelegate>
{
//定义数组、表格、底部视图
NSMutableArray *array;
UITableView *tableview;
UIView *bottomview;
}
[super viewDidLoad];
//设置导航标题
self.navigationItem.title = @"周宇航";
//设置背景颜色
self.view.backgroundColor = [UIColor whiteColor];
//创建消息对象1并初始化
Message *m1 = [[Message alloc]init];
m1.imageName = @"1 14.jpg";
m1.message = @"谢谢了";
m1.backgroudImageName = @"2 6.jpg";
//创建消息对象2并初始化
Message *m2 = [[Message alloc]init];
m2.imageName = @"1 18.jpg";
m2.message = @"客气";
m2.backgroudImageName = @"2 7.png";
m2.isMyWords = YES;
//初始化数组
array = [NSMutableArray arrayWithObjects:m1,m2, nil];
//创建表格
tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-444)];
//设置代理
tableview.dataSource = self;
tableview.delegate = self;
//加入当前视图
[self.view addSubview:tableview];
//创建底部视图
bottomview = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-88, self.view.frame.size.width, 88)];
//加入当前视图
[self.view addSubview:bottomview];
//创建滚动文本框
UITextView *textview = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, self.view.frame.size.width-20, 44)];
//设置代理
textview.delegate = self;
//设置背景颜色
textview.backgroundColor = [UIColor cyanColor];
//设置键盘类型
textview.returnKeyType = UIReturnKeySend;
//加入底部视图
[bottomview addSubview:textview];
//创建工具栏
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(10, 44, self.view.frame.size.width-20, 44)];
//创建按钮1
UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"3.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toolbarItemClicked:)];
item1.tag = 10;
//创建按钮2
UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"4.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toolbarItemClicked:)];
item2.tag = 11;
//创建按钮3
UIBarButtonItem *item3 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"5.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toolbarItemClicked:)];
item3.tag = 12;
//创建按钮4
UIBarButtonItem *item4 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"6.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toolbarItemClicked:)];
item4.tag = 13;
//创建按钮5
UIBarButtonItem *item5 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"4.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toolbarItemClicked:)];
item5.tag = 14;
//创建按钮6
UIBarButtonItem *item6 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"3.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toolbarItemClicked:)];
item6.tag = 15;
//添加按钮到导航条
self.navigationItem.rightBarButtonItems = @[item5,item6];
//加入工具条
toolbar.items = @[item1,item2,item3,item4];
//加入底部视图
[bottomview addSubview:toolbar];
}
#pragma mark UITableViewDataSource
//设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count;
}
//设置单元格内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//创建静态字符串
static NSString *string = @"Cell";
//根据字符串查找可复用单元格
MyTableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:string];
//创建复用池
if (!cell) {
cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
}
//获取消息对象
Message *m = array[indexPath.row];
//设置头像视图
cell.imageview.image = [UIImage imageNamed:m.imageName];
//设置消息
cell.label.text = m.message;
//设置消息背景视图
cell.backgroudImageView.image = [UIImage imageNamed:m.backgroudImageName];
if (m.isMyWords == YES) {
cell.imageview.frame = CGRectMake(self.view.frame.size.width-44, 0, 34, 34);
cell.backgroudImageView.frame = CGRectMake(self.view.frame.size.width-130, 0, 80, 34);
}
return cell;
}
#pragma mark UITextViewDelegate
//设置点击滑动文本框响应方法
- (void)textViewDidBeginEditing:(UITextView *)textView
{
//开始动画
[UIView beginAnimations:@"up" context:nil];
//设置持续时间
[UIView setAnimationDuration:0.25];
//设置视图位置
[bottomview setFrame:CGRectMake(0, self.view.frame.size.height-380, self.view.frame.size.width, 380)];
//结束动画
[UIView commitAnimations];
}
//设置编辑响应方法
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
//换行的时候
if ([text isEqualToString: @"\n"])
{
//设置键盘收回
[self.view endEditing:YES];
//开始动画
[UIView beginAnimations:@"down" context:nil];
//设置持续时间
[UIView setAnimationDuration:0.25];
//设置视图位置
[bottomview setFrame:CGRectMake(0, self.view.frame.size.height-88, self.view.frame.size.width, 88)];
//结束动画
[UIView commitAnimations];
//消息非空
if (textView.text.length != 0) {
//创建消息对象
Message *m = [[Message alloc]init];
//给消息赋值
m.imageName = @"1 18.jpg";
m.message = textView.text;
m.backgroudImageName = @"2 7.png";
m.isMyWords = YES;
//加入数组
[array addObject:m];
//刷新表格
[tableview reloadData];
//显示表格底部
[tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:array.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
textView.text = nil;
}
return NO;
}else{
return YES;
}
}
//设置工具条按钮响应方法
- (void)toolbarItemClicked:(UIBarButtonItem *)item
{
NSLog(@"%ld",item.tag);
}
创建MyTableViewCell继承UITableViewCell
.h
//定义属性头像视图、背景图片视图、标签
@property (nonatomic,strong)UIImageView *imageview;
@property (nonatomic,strong)UIImageView *backgroudImageView;
@property (nonatomic,strong)UILabel *label;
.m
//重写初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self addSubview:self.imageview];
[self addSubview:self.backgroudImageView];
}
return self;
}
//初始化头像视图
- (UIImageView *)imageview
{
if (!_imageview) {
_imageview = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 34, 34)];
//设置半径
_imageview.layer.cornerRadius = 17;
//设置边框模式
_imageview.layer.masksToBounds = YES;
}
return _imageview;
}
//初始化背景图片视图
- (UIImageView *)backgroudImageView
{
if (!_backgroudImageView) {
_backgroudImageView = [[UIImageView alloc]initWithFrame:CGRectMake(60, 5, 80, 34)];
//设置半径
_backgroudImageView.layer.cornerRadius = 10;
//设置边框模式
_backgroudImageView.layer.masksToBounds = YES;
//创建标签
_label = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 60, 24)];
//加入图片视图
[self.backgroudImageView addSubview:self.label];
}
return _backgroudImageView;
}
创建Message继承NSObject
.h
//定义属性头像、消息、背景图片、发送人
@property (nonatomic,strong)NSString *imageName;
@property (nonatomic,strong)NSString *message;
@property (nonatomic,strong)NSString *backgroudImageName;
@property (nonatomic,assign)BOOL isMyWords;