#import "AppDelegate.h"
.h
//創(chuàng)建頁面視圖
ViewController *vc = [[ViewController alloc]init];
//創(chuàng)建導(dǎo)航器對象
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
//修改窗口根視圖
self.window.rootViewController = nav;
#import "ViewController.h"
.h
#import "Message.h"
#import "MyTableViewCell.h"
<UITableViewDataSource,UITableViewDelegate>
{
//定義數(shù)組燃箭、表格钉答、底部視圖
NSMutableArray *array;
UITableView *tableview;
UIView *bottomview;
}
[super viewDidLoad];
//設(shè)置導(dǎo)航標(biāo)題
self.navigationItem.title = @"周宇航";
//設(shè)置背景顏色
self.view.backgroundColor = [UIColor whiteColor];
//創(chuàng)建消息對象1并初始化
Message *m1 = [[Message alloc]init];
m1.imageName = @"1 14.jpg";
m1.message = @"謝謝了";
m1.backgroudImageName = @"2 6.jpg";
//創(chuàng)建消息對象2并初始化
Message *m2 = [[Message alloc]init];
m2.imageName = @"1 18.jpg";
m2.message = @"客氣";
m2.backgroudImageName = @"2 7.png";
m2.isMyWords = YES;
//初始化數(shù)組
array = [NSMutableArray arrayWithObjects:m1,m2, nil];
//創(chuàng)建表格
tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-444)];
//設(shè)置代理
tableview.dataSource = self;
tableview.delegate = self;
//加入當(dāng)前視圖
[self.view addSubview:tableview];
//創(chuàng)建底部視圖
bottomview = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-88, self.view.frame.size.width, 88)];
//加入當(dāng)前視圖
[self.view addSubview:bottomview];
//創(chuàng)建滾動文本框
UITextView *textview = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, self.view.frame.size.width-20, 44)];
//設(shè)置代理
textview.delegate = self;
//設(shè)置背景顏色
textview.backgroundColor = [UIColor cyanColor];
//設(shè)置鍵盤類型
textview.returnKeyType = UIReturnKeySend;
//加入底部視圖
[bottomview addSubview:textview];
//創(chuàng)建工具欄
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(10, 44, self.view.frame.size.width-20, 44)];
//創(chuàng)建按鈕1
UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"3.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toolbarItemClicked:)];
item1.tag = 10;
//創(chuàng)建按鈕2
UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"4.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toolbarItemClicked:)];
item2.tag = 11;
//創(chuàng)建按鈕3
UIBarButtonItem *item3 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"5.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toolbarItemClicked:)];
item3.tag = 12;
//創(chuàng)建按鈕4
UIBarButtonItem *item4 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"6.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toolbarItemClicked:)];
item4.tag = 13;
//創(chuàng)建按鈕5
UIBarButtonItem *item5 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"4.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toolbarItemClicked:)];
item5.tag = 14;
//創(chuàng)建按鈕6
UIBarButtonItem *item6 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"3.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toolbarItemClicked:)];
item6.tag = 15;
//添加按鈕到導(dǎo)航條
self.navigationItem.rightBarButtonItems = @[item5,item6];
//加入工具條
toolbar.items = @[item1,item2,item3,item4];
//加入底部視圖
[bottomview addSubview:toolbar];
}
#pragma mark UITableViewDataSource
//設(shè)置行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count;
}
//設(shè)置單元格內(nèi)容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//創(chuàng)建靜態(tài)字符串
static NSString *string = @"Cell";
//根據(jù)字符串查找可復(fù)用單元格
MyTableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:string];
//創(chuàng)建復(fù)用池
if (!cell) {
cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
}
//獲取消息對象
Message *m = array[indexPath.row];
//設(shè)置頭像視圖
cell.imageview.image = [UIImage imageNamed:m.imageName];
//設(shè)置消息
cell.label.text = m.message;
//設(shè)置消息背景視圖
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
//設(shè)置點擊滑動文本框響應(yīng)方法
- (void)textViewDidBeginEditing:(UITextView *)textView
{
//開始動畫
[UIView beginAnimations:@"up" context:nil];
//設(shè)置持續(xù)時間
[UIView setAnimationDuration:0.25];
//設(shè)置視圖位置
[bottomview setFrame:CGRectMake(0, self.view.frame.size.height-380, self.view.frame.size.width, 380)];
//結(jié)束動畫
[UIView commitAnimations];
}
//設(shè)置編輯響應(yīng)方法
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
//換行的時候
if ([text? isEqualToString: @"\n"])
{
//設(shè)置鍵盤收回
[self.view endEditing:YES];
//開始動畫
[UIView beginAnimations:@"down" context:nil];
//設(shè)置持續(xù)時間
[UIView setAnimationDuration:0.25];
//設(shè)置視圖位置
[bottomview setFrame:CGRectMake(0, self.view.frame.size.height-88, self.view.frame.size.width, 88)];
//結(jié)束動畫
[UIView commitAnimations];
//消息非空
if (textView.text.length != 0) {
//創(chuàng)建消息對象
Message *m = [[Message alloc]init];
//給消息賦值
m.imageName = @"1 18.jpg";
m.message = textView.text;
m.backgroudImageName = @"2 7.png";
m.isMyWords = YES;
//加入數(shù)組
[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;
}
}
//設(shè)置工具條按鈕響應(yīng)方法
- (void)toolbarItemClicked:(UIBarButtonItem *)item
{
NSLog(@"%ld",item.tag);
}
創(chuàng)建MyTableViewCell繼承UITableViewCell
.h
//定義屬性頭像視圖肉微、背景圖片視圖、標(biāo)簽
@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)];
//設(shè)置半徑
_imageview.layer.cornerRadius = 17;
//設(shè)置邊框模式
_imageview.layer.masksToBounds = YES;
}
return _imageview;
}
//初始化背景圖片視圖
- (UIImageView *)backgroudImageView
{
if (!_backgroudImageView) {
_backgroudImageView = [[UIImageView alloc]initWithFrame:CGRectMake(60, 5, 80, 34)];
//設(shè)置半徑
_backgroudImageView.layer.cornerRadius = 10;
//設(shè)置邊框模式
_backgroudImageView.layer.masksToBounds = YES;
//創(chuàng)建標(biāo)簽
_label = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 60, 24)];
//加入圖片視圖
[self.backgroudImageView addSubview:self.label];
}
return _backgroudImageView;
}
創(chuàng)建Message繼承NSObject
.h
//定義屬性頭像京腥、消息、背景圖片鼻百、發(fā)送人
@property (nonatomic,strong)NSString *imageName;
@property (nonatomic,strong)NSString *message;
@property (nonatomic,strong)NSString *backgroudImageName;
@property (nonatomic,assign)BOOL isMyWords;