首页 > 其他 > 详细

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

时间:2015-08-11 07:14:39      阅读:317      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!