简单聊天界面

news/2024/7/20 20:22:02 标签: 聊天, iOS, 聊天界面, delegate, 控件

1.在Main.storyboard中创建控制器和控件,添加约束,设置TextField 如图:



2.为TableView在控制器上添加delegate、datasource,为textField添加delegate

            

3.在ViewController.m中添加代码

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutConstraint;//UIView的底部约束,随着键盘弹出或者收缩而改变

@property (nonatomic,strong) NSMutableArray *msgArray;//保存输入的字符串

@end
@implementation ViewController

//对可变数组进行懒加载
-(NSMutableArray *)msgArray{
    if (!_msgArray) {
        _msgArray = [NSMutableArray array];
    }
    return _msgArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//添加通知,监控键盘的变化
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

-(void)keyboardChange:(NSNotification *)notification{
    NSLog(@"%@",notification.userInfo);
    
    CGRect keyFrame = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];//获取键盘的Frame
    
    CGFloat keyFrameY = keyFrame.origin.y;//获取键盘的坐标Y
    
    self.layoutConstraint.constant = [UIScreen mainScreen].bounds.size.height - keyFrameY;//UIView底部的约束
}

#param mark - UITextfieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.msgArray addObject:textField.text];
    //发送之后,使输入框内容为空
    textField.text = nil;
    //加载表视图
    [self.tableView reloadData];
    
    NSIndexPath *index = [NSIndexPath indexPathForRow:(self.msgArray.count-1) inSection:0];
    //始终滚动出最新一行消息
    [self.tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    
    return YES;
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//滚动表视图时,键盘收起
    [self.view endEditing:YES ];
}

#param mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.msgArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MsgCell" forIndexPath:indexPath];
    
    cell.textLabel.text = [self.msgArray objectAtIndex:indexPath.row];
    
    return cell;
}




http://www.niftyadmin.cn/n/704468.html

相关文章

【杂文】CM 倒下了,但还有这些第三方安卓 ROM 让你刷个痛快

写在前面&#xff1a;本文内容转载于微信公众号 雷科技 并已经获得作者授权&#xff0c;若需要转载&#xff0c;请联系 雷科技 对于 Android 发烧友而言&#xff0c;最近最为重磅的消息就莫过于第三方 Android ROM 之首的 CyanogenMod&#xff08;简称 CM&#xff09;宣布停止固…

CocoaPod的安装

更新以前大家都是根据淘宝镜像来安装cocoapods&#xff0c;此篇文章也和大多数博文内容相仿。后来换电脑时&#xff0c;同事说淘宝镜像已经不能用了&#xff0c;网上又重新查看教程&#xff0c;确实都是这么说的&#xff01;&#xff08;好吧&#xff0c;我确实比较懒&#xff…

CocoaPod的使用

CocoaPod的安装 安装CocoaPods成功之后&#xff0c;首先会找一些第三方库以及最新版本&#xff08;要知道想用什么库才能搜索&#xff09; 1.寻找第三方库是否存在&#xff0c;以及版本 &#xff08;Masonry是第三方库名称&#xff09; pod search Masonry 2.使用cd 指令&a…

Python读取大文件的坑“与内存占用检测

python读写文件的api都很简单&#xff0c;一不留神就容易踩”坑“。笔者记录一次踩坑历程&#xff0c;并且给了一些总结&#xff0c;希望到大家在使用python的过程之中&#xff0c;能够避免一些可能产生隐患的代码。1.read()与readlines()&#xff1a; 随手搜索python读写文件的…

iOS 获取系统当前时间

获取系统当前时间&#xff1a; NSDate *currentDate[NSDate date];NSDateFormatter *dateformatter[[NSDateFormatter alloc]init];[dateformatter setDateFormat:"YYYY-MM-dd HH:mm:ss"];NSString *currentTime[dateformatter stringFromDate:currentDate];NSLog(&q…

同服务器 内的 不同数据库对象之间的对比

以下 SQL 只适用于 同服务器 内的 不同数据库 之间的对比&#xff0c;如果是不同的服务器&#xff0c;可以把 服务器A 中的目标数据库备份后&#xff0c;以另一个名字恢复到 服务器B 中 1、u表&#xff0c;p存储过程&#xff0c;v视图 的存在性对比1&#xff09;表sysobjects.u…

iOS开发系列--数据存取

数据存取 http://www.cnblogs.com/kenshincui/p/4077833.html 概览 在iOS开发中数据存储的方式可以归纳为两类&#xff1a;一类是存储为文件&#xff0c;另一类是存储到数据库。例如前面 IOS开发系列—Objective-C之Foundation框架 的文章中提到归档、plist文件存储&#xf…

微服务架构的核心要点和实现原理解析

摘要&#xff1a;本文中&#xff0c;我们将进一步理解微服务架构的核心要点和实现原理&#xff0c;为读者的实践提供微服务的设计模式&#xff0c;以期让微服务在读者正在工作的项目中起到积极的作用。微服务架构中职能团队的划分传统单体架构将系统分成具有不同职责的层次&…