您的位置:首页 > 移动开发 > Objective-C

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];
//

}];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: