iOS - 项目开发相关

2020-04-09  本文已影响0人  风之化身呀

1.主目录按照业务分类,内目录按照模块分类
2.主目录按照模块分类,内目录按照业务分类(推荐)


1、网络请求 AFNetworking
2、图片下载与缓存 SDWebImage
3、JSON 格式化 JSONModel
4、Flex 布局库 Yoga
5、UICollectionView 框架 IGListKit
6、日志库 CocoaLumberjack

//保存NSInteger
[defaults setInteger:(NSInteger) forKey:(nonnull NSString *)];
//保存BOOL
[defaults setBool:(BOOL) forKey:(nonnull NSString *)];
// 保存对象
[defaults setObject:@"用户名" forKey:kUsernameKey];

//读取NSInteger
[defaults setInteger:(NSInteger) forKey:(nonnull NSString *)];
//读取BOOL
[defaults setBool:(BOOL) forKey:(nonnull NSString *)];
// 读取对象
NSString *username = [defaults objectForKey:kUsernameKey];

//删除指定key的数据
[defaults removeObjectForKey:(nonnull NSString *)];
[UIScreen mainScreen].bounds.size  //  屏幕宽高
[UIColor redColor]  // 颜色
_titleLabel = [[UILabel alloc] init];
_titleLabel.text=@"欢迎使用";
_titleLabel.textColor = [UIColor blackColor];
_titleLabel.font = [UIFont systemFontOfSize:23];

// 第二种方式
UILabel *label = [UILabel new];
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] 
                                   initWithString:@"127"];
[text addAttribute:NSKernAttributeName 
             value:@-0.5 
             range:NSMakeRange(0, text.length)];
[label setAttributedText:text];
// 图片要放到 Assets.xcassets 里
UIImage *image= [UIImage imageNamed:@"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.backgroundColor = [UIColor clearColor];
[button1 setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal];
_button.layer setMasksToBounds:YES];
[_button.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
[button1 setTitle:@"点击" forState:UIControlStateNormal];
[button1.titleLabel setFont:[UIFont systemFontOfSize:40]];
[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];

1、Controller需要实现两个 delegate ,分别是 UITableViewDelegate 和 UITableViewDataSource

// GTNewsViewController.m
@interface GTNewsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong, readwrite) UITableView *tableView;
@property (nonatomic, strong, readwrite) NSArray *dataArray;
@end

2、然后 UITableView对象的 delegate要设置为 self

_tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];

3、然后就可以实现这些delegate的一些方法。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 10;
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;              
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;   
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

// 返回指定的row 的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";
    UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];
    if (cell == nil){
        // Create a cell to display an ingredient.
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                       reuseIdentifier:showUserInfoCellIdentifier] 
                autorelease];
    }
    // Configure the cell.
    cell.textLabel.text=@"签名";
    cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str()  encoding:NSUTF8StringEncoding];
        }

// 响应行点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        return;
    }
    else if(indexPath.section==0){
        switch (indexPath.row) {
            //聊天
            case 0:{
                [self  onTalkToFriendBtn];
            }
                break;
            default:
                break;
        }
    }
    else {
        return ;
    }
}

以下两句容易忘记
1、layout.isEnabled =YES
2、[self.view.yoga applyLayoutPreservingOrigin:NO]

// 父级 view 设置垂直居中
    self.view.backgroundColor = [UIColor greenColor];
    [self.view configureLayoutWithBlock:^(YGLayout *_Nonnull layout) {
        layout.isEnabled = YES;
        layout.alignItems = YGAlignCenter;
        layout.justifyContent = YGJustifyCenter;
    }];

    // 子view
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor redColor];
    [view configureLayoutWithBlock:^(YGLayout *layout) {
        layout.isEnabled = YES;
        layout.width = YGPointValue(100);
        layout.height = YGPointValue(100);
        layout.flexDirection = YGFlexDirectionRow;
        layout.alignItems = YGAlignCenter;
        layout.justifyContent = YGJustifyCenter;
    }];
    [self.view addSubview:view];

    // 嵌套子 view
    [view addSubview:({
        UIView *view = [[UIView alloc] init];
        [view configureLayoutWithBlock:^(YGLayout *_Nonnull layout) {
            layout.isEnabled = YES;
            layout.width = YGPointValue(20);
            layout.height = YGPointValue(20);
        }];
        view.backgroundColor = [UIColor blackColor];
        view;
    })];

    [self.view.yoga applyLayoutPreservingOrigin:NO];

一些问题

在.h文件中声明的属性,外部类可以通过“类实例.属性”来调用,
但在.m中声明的则不可以,获取和设置的方法,只能是通过setValue:forKey和valueForKey来实现。属于私有的,子类不可访问。

atomatic 读写安全,线程不安全
nonatomatic 速度快
strong 强引用
copy 深度复制一份
weak 若引用
readonly 只读
readwrite 可读可写
更多

1、Model 写法

//.h声明
#import <Foundation/Foundation.h>
//第一个model
@interface NewModel : NSObject
@property(nonatomic,copy)NSString *familyName;//姓氏
@property(nonatomic,strong)NSArray *messageArray;//信息
- (instancetype)initWithDic:(NSDictionary *)dic;
@end

//第二个model
@interface NewModel2 : NSObject
@property(nonatomic,copy)NSString *name;//姓名
@property(nonatomic,copy)NSString *sex;//性别
- (instancetype)initWithDic:(NSDictionary *)dic;
@end

//.m实现
#import "NewModel.h"
// 第一个model
@implementation NewModel
/**
 *  构造
 *
 *  @param dic <#dic description#>
 *
 *  @return <#return value description#>
 */
- (instancetype)initWithDic:(NSDictionary *)dic
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dic];
        //创建一个可变数组加载soldarray
        NSMutableArray *newArray = [NSMutableArray array];
        for (NSDictionary *dic in self.messageArray) {
            NewModel2 *model = [[NewModel2 alloc]initWithDic:dic];
            [newArray addObject:model];
        }
        self.messageArray = newArray;
    }
    return self;
}
@end

//第二个model
@implementation NewModel2
/**
 *  构造
 *
 *  @param dic <#dic description#>
 *
 *  @return <#return value description#>
 */
- (instancetype)initWithDic:(NSDictionary *)dic
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}
@end

2、View 写法

// example.m
@interface GTDeleteCellView ()
@property (nonatomic, strong, readwrite) UIView *backgroundView;
@property (nonatomic, strong, readwrite) UIButton *deleteButton;
@property (nonatomic, copy, readwrite) dispatch_block_t deleteBlock;
@end

@implementation GTDeleteCellView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:({
            _backgroundView = [[UIView alloc] initWithFrame:self.bounds];
            _backgroundView.backgroundColor = [UIColor blackColor];
            _backgroundView.alpha  = 0.5;
            [_backgroundView addGestureRecognizer:({
                UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDeleteView)];
                tapGesture;
            })];
            _backgroundView;
        })];
        self.clipsToBounds = YES;
    }
    return self;
}
@end

注意:定义的私有属性可通过 _ 的形式访问
3、Controller 写法

//.h声明
#import <UIKit/UIKit.h>

@interface NewViewController : UIViewController
@property(nonatomic,weak)UITableView *tableView;
@property(nonatomic,strong)NSArray *foldArray;//数据
@end

//.m实现
#import "NewViewController.h"
#import "NewModel.h"
@interface NewViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation NewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //加载表
    [self loadTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 *  数据加载
 */
-(NSArray *)foldArray
{
    if (_foldArray == nil) {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"data.plist" ofType:nil];
        NSArray *oldArray = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *newArray = [NSMutableArray array];
        for (NSDictionary *dic in oldArray) {
            NewModel *model = [[NewModel alloc]initWithDic:dic];
            [newArray addObject:model];
        }
        _foldArray = newArray;
    }
    return _foldArray;
}
/**
 *  加载表的方法
 */
- (void)loadTableView
{
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    self.tableView = tableView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.foldArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NewModel *fModel = self.foldArray[section];
    return fModel.messageArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    NewModel *fModel = self.foldArray[indexPath.section];
    NewModel2 *sModel = fModel.messageArray[indexPath.row];
    cell.textLabel.text = sModel.name;
    cell.detailTextLabel.text = sModel.sex;
    return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NewModel *fModel = self.foldArray[section];
    return fModel.familyName;
}
@end
上一篇下一篇

猜你喜欢

热点阅读