46.小项目:QQ聊天界面 C部分

---------- ViewController.m ----------

#import "ViewController.h"

#import "CZMessageCell.h"

#import "CZMessage.h"

#import "CZMessageFrame.h"


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

@property (weak, nonatomic) IBOutlet UITextField *inputField;

@property (strong, nonatomic) NSMutableArray *messageFrames;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end


@implementation ViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

    self.tableView.backgroundView = nil;

    self.tableView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

    self.inputField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 0)];

    self.inputField.leftViewMode  = UITextFieldViewModeAlways;

}


- (void)dealloc

{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}


- (NSMutableArray *)messageFrames

{

    if (_messageFrames == nil)

    {

        NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"messages.plist" ofType:nil]];

        NSMutableArray *mfArray = [NSMutableArray array];

        for (NSDictionary *dict in dictArray) {

            CZMessage *msg = [CZMessage messageWithDict:dict];

            CZMessageFrame *lastFrame = [mfArray lastObject];

            CZMessage *lastMsg = lastFrame.message;

            msg.hiddenTime = [lastMsg.time isEqualToString:msg.time];

            CZMessageFrame *mf = [[CZMessageFrame alloc] init];

            mf.message = msg;

            [mfArray addObject:mf];

        }

        _messageFrames = mfArray;

    }

    return _messageFrames;

}


- (BOOL)prefersStatusBarHidden

{

    return YES;

}


- (void)addMsgWithText:(NSString *)text type:(CZMessageType)type

{

    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];

    fmt.dateFormat = @"HH:mm";

    CZMessage *msg = [[CZMessage alloc] init];

    msg.type = type;

    msg.text = text;

    msg.time = [fmt stringFromDate:[NSDate date]];

    CZMessageFrame *lastFrame = [self.messageFrames lastObject];

    CZMessage *lastMsg = lastFrame.message;

    msg.hiddenTime = [lastMsg.time isEqualToString:msg.time];

    CZMessageFrame *mf = [[CZMessageFrame alloc] init];

    mf.message = msg;

    [self.messageFrames addObject:mf];

    NSIndexPath *path = [NSIndexPath indexPathForRow:self.messageFrames.count - 1 inSection:0];

    [self.tableView reloadData];

    [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionBottom animated:YES];

}


- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [self addMsgWithText:textField.text type:CZMessageTypeMe];

    [self addMsgWithText:[NSString stringWithFormat:@"%@你个蛋啊", textField.text] type:CZMessageTypeOther];

    textField.text = nil;

    return YES;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return self.messageFrames.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    CZMessageCell *cell = [CZMessageCell cellWithTableView:tableView];

    cell.messageFrame = self.messageFrames[indexPath.row];

    return cell;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return [self.messageFrames[indexPath.row] cellHeight];

}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

    [self.view endEditing:YES];

}


- (void)keyboardWillChangeFrame:(NSNotification *)note

{

    self.view.window.backgroundColor = self.tableView.backgroundColor;

    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    CGFloat keyboardY = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;

    [UIView animateWithDuration:duration animations:^{

        CGFloat ty = keyboardY - self.view.frame.size.height;

        self.view.transform = CGAffineTransformMakeTranslation(0, ty);

    }];

}


@end

 

转载于:https://www.cnblogs.com/lixiang2015/p/4719813.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值