标签:
---------- 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
标签:
原文地址:http://www.cnblogs.com/lixiang2015/p/4719813.html