码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-11 07:14:54      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

---------- 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

 

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

标签:

原文地址:http://www.cnblogs.com/lixiang2015/p/4719813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!