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. Android Drawable、Bitmap、byte[]之间的转换

    转自http://blog.csdn.net/june5253/article/details/7826597 1.Bitmap-->Drawable Bitmap drawable2Bitma ...

  2. Android中Bitmap,byte[],Drawable相互转化

    一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...

  3. JSON入门指南--客户端处理JSON

    在传统的Web开发过程中,前端工程师或者后台工程师会在页面上写后台的相关代码,比如在ASP.NET MVC4里面写如下代码: @Html.TextBoxFor(m => m.UserName, ...

  4. 在ASP.NET MVC的Action中直接接受客户端发送过来的HTML内容片段

    出于安全的考虑,默认情况下,如果从客户端发送过来的数据中直接包括了HTML内容,ASP.NET会自动启动保护措施,你会收到下面的错误提示 这当然是一个不错的设计,只不过在某些特殊的事情,如果我们确实需 ...

  5. 窥探Swift编程之别样的HelloWorld

    从今天就开始陆陆续续的发布一些有关Swift语言的东西,虽然目前在公司项目开发中Objective-C还是iOS开发的主力军,但是在不久的将来Swift将会成为iOS开发中的新生宠儿.所以在在Xcod ...

  6. Opencv摄像头实时人脸识别

    Introduction 网上存在很多人脸识别的文章,这篇文章是我的一个作业,重在通过摄像头实时采集人脸信息,进行人脸检测和人脸识别,并将识别结果显示在左上角. 利用 OpenCV 实现一个实时的人脸 ...

  7. JavaScript的三种工业化调试方法

    JavaScript的三种工业化玩法 软件工程中任何的语言如果想要写出健壮的代码都需要锋利的工具,当然JavaScript也不例外,很多朋友刚入门的时候往往因为工具选的不对而事半功倍,JavaScri ...

  8. GitHub托管BootStrap资源汇总(持续更新中…)

    Twitter BootStrap已经火过大江南北,对于无法依赖美工的程序员来说,这一成熟前卫的前端框架简直就一神器,轻轻松松地实现出专业的UI效果.GitHub上相关的的开源项目更是层出不穷,在此整 ...

  9. CSS3魔法堂:说说Multi-column Layout

    前言  是否记得<读者文摘>中那一篇篇优美感人的文章呢?那除了文章内容外,还记得那报刊.杂志独有的多栏布局吗?  当我们希望将报刊.杂志中的阅读体验迁移到网页上时,最简单直接的方式就是采用 ...

  10. 30天React Native从零到IOS/Android双平台发布总结

    前言 本人有近十年的技术背景,除了APP开发之外对后端.前端等都比较熟悉,近期做一个APP项目需要IOS.Android两个平台都需要,只能硬着头皮上.其实很早就想开发APP也很早就接触Android ...