iOS开发脚踏实地学习day08-QQ聊天界面

本文详细介绍了在Xcode开发环境中解决CGRect未知类型问题、获取文本框大小、隐藏状态栏、自定义Button控件设置、导入类的方法、创建tableViewCell、隐藏重复时间、图片拉伸、设置tableView背景色、键盘事件处理、模型数据添加等关键编程技巧。同时提供了代码示例和优化建议,旨在提高开发者在iOS应用开发过程中的效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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


4.自定义Button控件设置

-(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;
    
}


8.图片拉伸

//拉伸图片
-(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];
    

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值