您的位置:首页 > 其它

平时小积累

2015-11-01 13:32 190 查看
一:使用多线程来处理数据的存储

// 需要将照片保存至应用程序沙箱,由于涉及到数据存储,同时与界面无关,可以使用多线程来保存图像
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 保存图像
// 1. 去图像路径
NSArray *docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *imagePath = [docs[0]stringByAppendingPathComponent:@"abc.png"];

// 2. 转换成NSData保存
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:imagePath atomically:YES];
});


二:自定义cell里面实现重用机制,textLabel调

在自定义cell里面实现重用机制,这样看起来更容易
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *ID = @"lrc";
HMLrcCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[HMLrcCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
return cell;
}


textLabel可以调整位置的,只要自定义cell然后实现layoutSubviews方法,重新设置大小即可,而且在初始化方法里进行相关属性的设置。那么相应的detailTextLabel和图片子控件都是可以调节的,全部重新设置frame.

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor];//只要设置了cell的背景颜色和tableView的背景颜色为clearColor,那么就是透明的。
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.textLabel.textColor = [UIColor whiteColor];
self.textLabel.numberOfLines = 0;
self.textLabel.textAlignment = NSTextAlignmentCenter;
}
return self;
}


重新布局子视图

- (void)layoutSubviews
{
[super layoutSubviews];
self.textLabel.frame = self.bounds;
}


三:类方法中调用实例化方法只要初始化一个对象即可, ok!

+ (instancetype)heroWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}

- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: