1.Xcode unknown type name “CGRect”
解决方法;添加#import <UIKit/UIKit.h>
2.得到文本框大小
CGSize testMaxSize = CGSizeMake(150, MAXFLOAT);
CGSize textRealSize = [message.text <span style="background-color: rgb(255, 255, 0);">boundingRectWithSize</span>:testMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:kBtnSize} context:nil].size;
3.隐藏状态栏
在controller
//隐藏状态栏
-(BOOL)prefersStatusBarHidden{
return YES;
}
-(UIButton *)textButton{
if (_textButton == nil) {
_textButton = [[UIButton alloc] init];
_textButton.backgroundColor = [UIColor grayColor];//正文默认颜色是白色
_textButton.<span style="background-color: rgb(255, 255, 0);">titleLabel</span>.font = [UIFont systemFontOfSize:13.0f];//字体
_textButton.<span style="background-color: rgb(255, 255, 0);">titleLabel.numberOfLines</span> = 0;//自动换行,默认是一行
[self.contentView addSubview:_textButton];
}
return _textButton;
}
5.#import,#include和@class
http://www.xcoder.cn/index.php/archives/122
#include,可以导入,但不能判断是否重复导入,如果要判断,那么就需要使用#ifdef进行宏判断,非常麻烦!
#import方法是Objective-C引入的,跟#include意义上是一样的,但是可以自行判断是否重复导入,如果已经导入了,那么就忽略掉;
#import和#include导入类有两种方法:#import <>和#import “”。前面一个使用<>尖括号使用时,表示引入的是系统类,会直接到库里查找。如果使用””双引号,表示引入自定义类,系统会先查找当前目录,然后查找path目录,最后再在库中进行查找。
使用@Class+类名,其实只是告诉编译器,这是一个类,而不是加载类头文件,所以编译器不会处理这个类的结构,就不知道类具有的属性和方法。使用@class指令提高了程序效率,原因如上,编译器只需要知道有这样一个类名,而不处理细节。但是,在使用中就不能调用类的方法了,如果使用了,会出现如下错误:
Receiver type ‘Person’ for instance message is a forward declaration
Proerity ‘name’ cannot be found in forward class object ‘Person’
6.代码写tableViewCell
//*********************代码创建cell******************************************************
Controller
->私有成员
@property(nonatomic,strong) MessageCell *cell;
->用类方法实例化cell
MessageCell *cell = [MessageCell messageCellWithTableView:tableView];
View
->类方法
+(instancetype)messageCellWithTableView:(UITableView *)tableView;
//***********************数据**************************************************************
Controller
->私有成员
@property(nonatomic,strong) NSMutableArray *messages;
使用懒加载,类方法实例化messages
-(NSMutableArray *)messages{
if (_messages == nil) {
_messages = [MessageModule messages];
}
return _messages;
}
Module
->字典转模型,类方法
+(NSMutableArray *)messages;
//************************cell的内容和frame************************************************
Controller
//下面这句话,先得到cell的message,再设置cell的frame,也就是所cell有两个需要得到的一个单元格要显示的内容,和一个单元格的布局。
MessageFrameModule *frameModule = self.messages[indexPath.row];
cell.frameMessage = frameModule;//进入frameMessage的setter方法
View
->成员
@property(nonatomic,strong) MessageFrameModule *frameMessage;
->成员的setter方法
-(void)setFrameMessage:(MessageFrameModule *)frameMessage{...}
7.隐藏重复的时间
MessageModule加一个属性hideTime,调用setter方法时,作为输入message的参数引入,来修改time的frame。
+(NSMutableArray *)messages{
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"messages.plist" ofType:nil]];
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in array) {
//以下写法,多次用到frameModule.message,会多次重复调用setter方法,非常不好,而且要改的是_输入的message的hideTime属性,而不是frameModule.message的hideTime属性,不能实现隐藏重复时间。
// MessageFrameModule *frameModule = [[MessageFrameModule alloc] init];
// frameModule.message = [self messageWithDict:dict];
// MessageFrameModule *frameLastModule = [arrayM lastObject];
// frameModule.message.hideTime = [frameModule.message.time isEqualToString:frameLastModule.message.time];
// frameModule.message.hideTime = YES;
// frameModule.message.text = @"病毒病毒";
// [arrayM addObject:frameModule];s
MessageModule *message = [MessageModule messageWithDict:dict];
MessageFrameModule *lastFrameModule = [arrayM lastObject];
message.hideTime = [message.time isEqualToString:lastFrameModule.message.time];
MessageFrameModule *frameModule = [[MessageFrameModule alloc] init];
frameModule.message = message;
[arrayM addObject:frameModule];
}
return arrayM;
}
//拉伸图片
-(UIImage *)resizableImage:(UIImage *)normalImage{
CGFloat top = normalImage.size.width*0.5 -1;
CGFloat left = normalImage.size.height*0.5-1;
CGFloat bottom = normalImage.size.width*0.5 +1;
CGFloat right = normalImage.size.height*0.5 +1;
//resizableImageWithCapInsets图片拉伸
//其中Insets这个参数的格式是(top,left,bottom,right),从上、左、下、右分别在图片上画了一道线,这样就给一个图片加了一个框。只有在框里面的部分才会被拉伸,而框外面的部分则不会改变。
UIImage *resizableImage = [normalImage resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right)];
return resizableImage;
}
9.设置tableView的背景色,一定要清空cell的背景色
cell.backgroundColor = [UIColor clearColor];//把cell的背景颜色去掉,这样tableView的颜色才能显现出来。
self.tableView.backgroundColor = [UIColor colorWithRed:225/255.0f green:225/255.0f blue:225/255.0f alpha:1.0];//注意255/255结果是0,不是小数,所以要加.0f
10.拉伸图片的方法
//拉伸图片
-(UIImage *)resizableImage:(UIImage *)normalImage{
CGFloat top = normalImage.size.width*0.5 -1;
CGFloat left = normalImage.size.height*0.5-1;
CGFloat bottom = normalImage.size.width*0.5 +1;
CGFloat right = normalImage.size.height*0.5 +1;
UIImage *resizableImage =[normalImage <span style="background-color: rgb(255, 255, 0);">stretchableImageWithLeftCapWidth</span>:left topCapHeight:top];//就是只要给左和上的距离,画出大小为1的框,只有这个框被拉伸,框外面的则都不会。
//resizableImageWithCapInsets图片拉伸
//其中Insets这个参数的格式是(top,left,bottom,right),从上、左、下、右分别在图片上画了一道线,这样就给一个图片加了一个框。只有在框里面的部分才会被拉伸,而框外面的部分则不会改变。
// UIImage *resizableImage = [normalImage <span style="background-color: rgb(255, 255, 0);">resizableImageWithCapInsets</span>:UIEdgeInsetsMake(top, left, bottom, right)];
return resizableImage;
}
11.键盘事件相关
//键盘frame将变化时的事件
-(void)<span style="background-color: rgb(255, 255, 0);">keyboardWillChangeFrame</span>:(NSNotification *)noti{
// NSLog(@"%@",noti.userInfo);
//把window的背景色改为和tableView一样
self.view.window.backgroundColor = self.tableView.backgroundColor;
CGRect frame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//CGAffineTransformMakeTranslation每次都是以最初位置的中心点为起始参照
#define kScreenH [[UIScreen mainScreen] bounds].size.height
CGFloat animationDuration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
[UIView animateWithDuration:animationDuration animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, frame.origin.y - kScreenH);}];
}
//当tableView在滚动的时候,结束编辑事件(退出键盘)
-(void)<span style="background-color: rgb(255, 255, 0);">scrollViewWillBeginDragging</span>:(UIScrollView *)scrollView{
[self.view endEditing:YES];
}
12.往模型上添加新的数据
- (void)addmessage:(NSString *)text type:(MessageModuleType)type{
//新的信息内容
MessageModule *newMessage = [[MessageModule alloc]init];
newMessage.text = text;
newMessage.time = @"11";
newMessage.type = type;
newMessage.hideTime = NO;
//新的frame
MessageFrameModule *newFrame = [[MessageFrameModule alloc]init];
newFrame.message = newMessage;
//添加到self.messages
[self.messages addObject:newFrame];
//刷新数据
[self.tableView reloadData];
//自动上移
NSIndexPath *path = [NSIndexPath indexPathForRow:self.messages.count-1 inSection:0];
[self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}