1:将自定义对象转化成NsData存入数据库

要转为nsdata自定义对象要遵循<NSCoding>的协议,然后实现encodeWithCoder,initwithcode对属性转化,实例如下:

HMShop.h
#import <Foundation/Foundation.h> @interface HMShop : NSObject <NSCoding>
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) double price;
@end HMShop.m
#import "HMShop.h" @implementation HMShop
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:self.name forKey:@"name"];
[encoder encodeDouble:self.price forKey:@"price"];
} - (id)initWithCoder:(NSCoder *)decoder
{
if (self = [super init]) {
self.name = [decoder decodeObjectForKey:@"name"];
self.price = [decoder decodeDoubleForKey:@"price"];
}
return self;
} - (NSString *)description
{
return [NSString stringWithFormat:@"%@ <-> %f", self.name, self.price];
}
@end 操作: - (void)addShops
{
NSMutableArray *shops = [NSMutableArray array];
for (int i = ; i<; i++) {
HMShop *shop = [[HMShop alloc] init];
shop.name = [NSString stringWithFormat:@"商品--%d", i];
shop.price = arc4random() % ; NSData *data = [NSKeyedArchiver archivedDataWithRootObject:shop];
[self.db executeUpdateWithFormat:@"INSERT INTO t_shop(shop) VALUES (%@);", data];
}
} - (void)readShops
{
FMResultSet *set = [self.db executeQuery:@"SELECT * FROM t_shop LIMIT 10,10;"];
while (set.next) {
NSData *data = [set objectForColumnName:@"shop"];
HMShop *shop = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"%@", shop);
}
} *把对象转成nsdata的理由,因为在存入数据库时会变成字符串,不利转化,所以先把其序列化转化成nsdata,然后存进数据库,取出时同样先为nsdata再转化;

2:增加子控制器,用来提取一些公共的内容布局,瘦身当前viewcontroller

DetailsViewController *details = [[DetailsViewController alloc] init];
details.photo = self.photo;
details.delegate = self;
[self addChildViewController:details];
CGRect frame = self.view.bounds;
frame.origin.y = ;
details.view.frame = frame;
[self.view addSubview:details.view];
[details didMoveToParentViewController:self];

3:用协议来分离出调用

在子控制器创建一个协议,然后在其内部对它进行处理传参

子控制器.h
@protocol DetailsViewControllerDelegate - (void)didSelectPhotoAttributeWithKey:(NSString *)key; @end @interface DetailsViewController : UITableViewController @property (nonatomic, strong) Photo *photo;
@property (nonatomic, weak) id <DetailsViewControllerDelegate> delegate; @end 子控制器.m - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = self.keys[(NSUInteger) indexPath.row];
//对它进行传参,让其在父控制器去实现
[self.delegate didSelectPhotoAttributeWithKey:key];
} 父控制器.m
@interface PhotoViewController () <DetailsViewControllerDelegate>
@end 然后(得到参数,进行原本子控制器要进行的操作):
- (void)didSelectPhotoAttributeWithKey:(NSString *)key
{
DetailViewController *detailViewController = [[DetailViewController alloc] init];
detailViewController.key = key;
[self.navigationController pushViewController:detailViewController animated:YES];
}

4:关于kvo的运用

//进度值改变 增加kvo 传值 key为fractionCompleted
- (void)setProgress:(NSProgress *)progress{
if (_progress) {
[_progress removeObserver:self forKeyPath:@"fractionCompleted"];
}
_progress = progress;
if (_progress) {
[_progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:nil];
}
}
//消息kvo消息
- (void)dealloc{
if (_progress) {
[_progress removeObserver:self forKeyPath:@"fractionCompleted"];
}
_progress = nil;
} #pragma mark KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if ([keyPath isEqualToString:@"fractionCompleted"]) {
NSProgress *progress = (NSProgress *)object;
NSProgress *cellProgress = _offsourecebean.cDownloadTask.progress;
BOOL belongSelf = NO;
if (cellProgress && cellProgress == progress) {
belongSelf = YES;
}
dispatch_async(dispatch_get_main_queue(), ^{
if (self) {
[self showProgress:progress belongSelf:belongSelf];
}
});
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
} *注意增加监听后在不用时要进行消除,移除观察,其中addObserver可以是其它对象,然后在其内部实现observeValueForKeyPath这个协议;增加监听时可以设置options类型,也可以多类型一起;比如NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld;当被监听的对象发生变化时,会马上通知监听对象,使它可以做出一些响应,比如视图的更新;

 5:自定义UITableViewCell的accessoryView 判断哪个Button按下

UITableview的开发中经常要自定义Cell右侧的AccessoryView,把他换成带图片的按钮,并在用户Tap时判断出是哪个自定义按钮被按下了。

创建自定义按钮,并设为AccessoryView
if (cell == nil) {
cell = [[UITableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; UIImage *image= [ UIImage imageNamed:@"delete.png" ];
UIButton *button = [ UIButton buttonWithType:UIButtonTypeCustom ];
CGRect frame = CGRectMake( 0.0 , 0.0 , image.size.width , image.size.height );
button. frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal ];
button. backgroundColor = [UIColor clearColor ];
[button addTarget:self action:@selector(buttonPressedAction forControlEvents:UIControlEventTouchUpInside];
cell. accessoryView = button;
} 如果将Button加入到cell.contentView中,也是可以的。
cell.contentView addSubview:button]; 在Tap时进行判断,得到用户Tap的Cell的IndexPath
- (void)buttonPressedAction id)sender
{
UIButton *button = (UIButton *)sender;
(UITableViewCell*)cell = [button superview];
int row = [myTable indexPathForCell:cell].row;
} 对于加到contentview里的Button
(UITableViewCell*)cell = [[button superview] superview];

6:直接运用系统自带的UITableViewCell,其中cell.accessoryView可以自定义控件

#import "MyselfViewController.h"

@interface MyselfViewController ()

@property (nonatomic, retain) NSMutableArray *datasource;

@end

@implementation MyselfViewController
-(void)dealloc {
[_datasource release];
[super dealloc];
} -(NSMutableArray *)datasource {
if (!_datasource) {
self.datasource = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"MyselfList" ofType:@"plist"]];
}
return _datasource;
} -(instancetype)init {
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) { }
return self;
} - (void)viewDidLoad {
[super viewDidLoad]; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
self.tableView.rowHeight = ;
self.navigationItem.title = @"我的";
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.datasource.count;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section.
return [self.datasource[section] count];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
NSDictionary *dict = [self.datasource[indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = dict[@"title"];
cell.imageView.image = [UIImage imageNamed:dict[@"imageName"]]; if (indexPath.section == && indexPath.row == ) {
cell.accessoryView = [[[UISwitch alloc] init] autorelease];
} else {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} return cell;
} @end

IOS开发基础知识--碎片15的更多相关文章

  1. IOS开发基础知识碎片-导航

    1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...

  2. IOS开发基础知识--碎片19

    1:键盘事件顺序 UIKeyboardWillShowNotification // 键盘显示之前 UIKeyboardDidShowNotification // 键盘显示完成后 UIKeyboar ...

  3. IOS开发基础知识--碎片33

    1:AFNetworking状态栏网络请求效果 直接在AppDelegate里面didFinishLaunchingWithOptions进行设置 [[AFNetworkActivityIndicat ...

  4. IOS开发基础知识--碎片40

    1:Masonry快速查看报错小技巧 self.statusLabel = [UILabel new]; [self.contentView addSubview:self.statusLabel]; ...

  5. IOS开发基础知识--碎片42

    1:报thread 1:exc_bad_access(code=1,address=0x70********) 闪退 这种错误通常是内存管理的问题,一般是访问了已经释放的对象导致的,可以开启僵尸对象( ...

  6. IOS开发基础知识--碎片50

      1:Masonry 2个或2个以上的控件等间隔排序 /** * 多个控件固定间隔的等间隔排列,变化的是控件的长度或者宽度值 * * @param axisType 轴线方向 * @param fi ...

  7. IOS开发基础知识--碎片3

    十二:判断设备 //设备名称 return [UIDevice currentDevice].name; //设备型号,只可得到是何设备,无法得到是第几代设备 return [UIDevice cur ...

  8. IOS开发基础知识--碎片11

    1:AFNetwork判断网络状态 #import “AFNetworkActivityIndicatorManager.h" - (BOOL)application:(UIApplicat ...

  9. IOS开发基础知识--碎片14

    1:ZIP文件压缩跟解压,使用ZipArchive 创建/添加一个zip包 ZipArchive* zipFile = [[ZipArchive alloc] init]; //次数得zipfilen ...

随机推荐

  1. 深入理解定位父级offsetParent及偏移大小

    前面的话 偏移量(offset dimension)是javascript中的一个重要的概念.涉及到偏移量的主要是offsetLeft.offsetTop.offsetHeight.offsetWid ...

  2. IOS数据存储之NSUserDefaults

    前言: 作为从事Android开发人来说一定听说过SharedPreferences,然后要成为一名ios开发工程师来说咋能不知道NSUserDefaults!接下来让我们认识一下. NSUserDe ...

  3. 从零开始编写自己的C#框架(8)——后台管理系统功能设计

    还是老规矩先吐下槽,在规范的开发过程中,这个时候应该是编写总体设计(概要设计)的时候,不过对于中小型项目来说,过于规范的遵守软件工程,编写太多文档也会拉长进度,一般会将它与详细设计合并到一起来处理,所 ...

  4. tn文本分析语言(三):高级语法

    标签(空格分隔): 未分类 高级操作 1.脚本表达式 用双引号包含的脚本被称为脚本表达式,目前支持嵌入Python. 脚本表达式只能在顺序表达式中使用.代码可以在三个位置存在: |位置|功能|例子| ...

  5. 【tomcat】不同域名解析到同一tomcat不同项目上

    问题: 1. 有多个域名,想输入的每个域名只能访问其中的一个项目 2. 这些项目都部署在同一个tomcat上的 解决步骤:      1.首先把所有域名都解析到这台服务器上,解析时只能填写ip地址,不 ...

  6. golang中的类和接口的使用

    类使用:实现一个people中有一个sayhi的方法调用功能,代码如下: type People struct { //.. } func (p *People) SayHi() { fmt.Prin ...

  7. C++ 使用 opencv 库时 Point 在已经引入了 core.hpp 的情况下仍无法识别的可能原因

    引入了 core.hpp 是不够的.请加上 using namespace cv;

  8. APP接口自动化测试JAVA+TestNG(一)之框架环境搭建

    前言 好久不曾写点啥,去年换到新公司组测试团队与培养建设花费大量时间与精力,终于架构成型与稳定有时间可以打打酱油了.很久没有总结点啥,提笔想写的内容太多,先放APP接口自动化的内容吧,这个估计大家比较 ...

  9. RAC学习笔记

    RAC学习笔记 ReactiveCocoa(简称为RAC),是由Github开源的一个应用于iOS和OS开发的新框架,Cocoa是苹果整套框架的简称,因此很多苹果框架喜欢以Cocoa结尾. 在学习Re ...

  10. 7.4 数据注解属性--Required

    Required attribute can be applied to a property of a domain class. EF Code-First will create a NOT N ...