牛骨文教育服务平台(让学习变的简单)
博文笔记

enumerateObjectsUsingBlock的使用问题记录

创建时间:2015-10-19 投稿人: 浏览次数:516

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

//

    }];



声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。