enumerateObjectsUsingBlock的使用问题记录
2015-10-19 15:34
267 查看
enumerateObjectsUsingBlock
说明:类似与for in 和for 循坏。效率高,运行时间短。
用法例子:
NSArray *_arData;
_arData = @[@"是1",
@"是2",
@"是3",
@"是4",
@"是5",
@"是6"];
//block引用内部变量定义规则
__block
float h =
self.view.frame.size.height*0.7/[_arData
count];
__block
float y =
0.15*self.view.frame.size.height;
[_arData
enumerateObjectsUsingBlock:^(NSString *obj,
NSUInteger idx,
BOOL *stop)
{
//
...............代码块处理....................(其中obj代表for循坏里面的每个字符串,idx代表索引下标,stop代表结束)
UIView *listV = [[UIView
alloc] initWithFrame:CGRectMake(0, y,
self.view.frame.size.width, h)];
[listV setBackgroundColor:[UIColor
clearColor]];
UILabel *l = [[UILabel
alloc] initWithFrame:CGRectMake(60,
0, listV.frame.size.width -
60, listV.frame.size.height)];
[l setFont:[UIFont
systemFontOfSize:20]];
[l setTextColor:[UIColor
whiteColor]];
[l setBackgroundColor:[UIColor
clearColor]];
[l
setText:obj];
l.userInteractionEnabled =
YES;
l.tag = idx;
[listV
addSubview:l];
[self.view
addSubview:listV];
y += h;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer
alloc]initWithTarget:self
action:@selector(tapAction:)];
singleTap.numberOfTapsRequired =
1;
[l
addGestureRecognizer:singleTap];
//
}];
说明:类似与for in 和for 循坏。效率高,运行时间短。
用法例子:
NSArray *_arData;
_arData = @[@"是1",
@"是2",
@"是3",
@"是4",
@"是5",
@"是6"];
//block引用内部变量定义规则
__block
float h =
self.view.frame.size.height*0.7/[_arData
count];
__block
float y =
0.15*self.view.frame.size.height;
[_arData
enumerateObjectsUsingBlock:^(NSString *obj,
NSUInteger idx,
BOOL *stop)
{
//
...............代码块处理....................(其中obj代表for循坏里面的每个字符串,idx代表索引下标,stop代表结束)
UIView *listV = [[UIView
alloc] initWithFrame:CGRectMake(0, y,
self.view.frame.size.width, h)];
[listV setBackgroundColor:[UIColor
clearColor]];
UILabel *l = [[UILabel
alloc] initWithFrame:CGRectMake(60,
0, listV.frame.size.width -
60, listV.frame.size.height)];
[l setFont:[UIFont
systemFontOfSize:20]];
[l setTextColor:[UIColor
whiteColor]];
[l setBackgroundColor:[UIColor
clearColor]];
[l
setText:obj];
l.userInteractionEnabled =
YES;
l.tag = idx;
[listV
addSubview:l];
[self.view
addSubview:listV];
y += h;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer
alloc]initWithTarget:self
action:@selector(tapAction:)];
singleTap.numberOfTapsRequired =
1;
[l
addGestureRecognizer:singleTap];
//
}];
相关文章推荐
- 第二章 object-c
- iPad 多屏处理
- SQL Server系统表sysobjects介绍与使用
- <constant name="struts.objectFactory" value="spring" />
- ObjectC&&Swift 渐变色算法实现
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- js查看Object对象的内容
- objective-c单例详解
- Objective-C 学习资料汇总
- 对象方法与类方法易错点
- 黑马程序员——ObjectiveC基础——对象作为方法的参数连续传递
- Object-C ---2
- 黑马程序员——ObjectiveC基础——封装、@property和点语法
- [Object C]_[初级]_[NSArray排序]
- django get_object_or_404
- 黑马程序员——ObjectiveC基础——继承与实例变量修饰符
- Objective-C Foundation框架实践——NSMutableDictionary(一)
- window.url.createobjecturl 兼容多种浏览器(IE,google,360,Safari,firefox)
- Objective-C Foundation框架实践——NSDictionary(一)
- EF学习和使用(八)你必须知道的效率问题根源之Expression<Func<TObject, bool>>与Func<TObject, bool>