1. iOS遍历数组的同时删除元素
NSMutableArray*array = [NSMutableArrayarrayWithArray:@[@"1",@"2",@"3",@"4",@"5"]];
[array enumerateObjectsUsingBlock:^(id_Nonnull obj, NSUInteger idx,BOOL* _Nonnull stop) {if([obj isEqualToString:@"3"]) {
[array removeObject:obj];
}
}];NSLog(@"%@",array);// 输出结果:(1,2,4,5)
NSMutableArray *array= [NSMutableArray arrayWithArray:@[@"1",@"2",@"3",@"4",@"5"]];for(inti =array.count -1; i >=0; i--) {
NSString *obj =array[i];if([obj isEqualToString:@"3"]) {
[arrayremoveObject:obj];
}
}
NSLog(@"%@",array);// 输出结果:(1,2,4,5)
2.根据文本内容自适应添加标签

//移除之前子视图
[_cityView.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[obj removeFromSuperview];
}];
CGFloat totalWith = zWIDTH - 117;
for (int i=0; i < titleArr.count;i++){
NSString * title = titleArr[i];
UIView *itemView = [[UIView alloc] init];
UILabel *lab = [[UILabel alloc] init];
lab.font = [UIFont systemFontOfSize:14.];
CGSize size = [title boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.]} context:nil].size;
CGFloat itemWith = size.width + 30 + 5;
if (i==0){
itemView.frame = CGRectMake(totalWith - itemWith, 0, itemWith, 35);
}else{
UIView * view = (UIView *)[_cityView.subviews lastObject];
itemView.frame = CGRectMake(view.frame.origin.x - itemWith - 10, view.frame.origin.y, itemWith, view.frame.size.height);
if (itemView.frame.origin.x < 0) {
itemView.frame = CGRectMake(totalWith - itemWith, view.frame.origin.y+view.frame.size.height + 5, itemWith, view.frame.size.height);
}
}
UILabel *cityLab = [Customlab TitleName:title FontSize:14. FontColor:@"333333"];
cityLab.backgroundColor = [CommonUtils colorWithHex:@"f5f5f5"];
cityLab.textAlignment = NSTextAlignmentCenter;
cityLab.numberOfLines = 1;
cityLab.userInteractionEnabled = YES;
[itemView addSubview:cityLab];
[cityLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.bottom.mas_equalTo(itemView);
make.top.mas_equalTo(itemView.mas_top).offset(5.);
make.right.mas_equalTo(itemView.mas_right).offset(-5.);
}];
cityLab.clipsToBounds = YES;
cityLab.layer.cornerRadius = 4;
UIImageView *removeImage = [[UIImageView alloc] init];
removeImage.image = [UIImage imageNamed:@"kplay_remove"];
[itemView addSubview:removeImage];
[removeImage mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.right.mas_equalTo(itemView);
make.size.mas_equalTo(CGSizeMake(16., 16.));
}];
UIButton *removeBtn = [[UIButton alloc] init];
removeBtn.backgroundColor = [UIColor clearColor];
[itemView addSubview:removeBtn];
removeBtn.tag = 100 + i;
[removeBtn addTarget:self action:@selector(removeCityLab:) forControlEvents:UIControlEventTouchUpInside];
[removeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.right.mas_equalTo(itemView);
make.size.mas_equalTo(CGSizeMake(22., 22.));
}];
[_cityView addSubview:itemView];
}
UIView * view = (UIView *)[_cityView.subviews lastObject];
CGRect frame = _cityView.frame;
frame.size.height = view.frame.origin.y + view.frame.size.height;
[_cityView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(frame.size.height);
}];
model.height_city = frame.size.height;
3.在执行刷新动作的时候,切换tab再切回来,刷新头回不去
网上找的答案: https://github.com/CoderMJLee/MJRefresh/issues/892
把mjrefreshheader.m中
(void)scrollViewContentOffsetDidChange:(NSDictionary *)change方法中的
if(self.window == nil) return;注释掉应该没问题了
我自己的答案:
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
if (self.tableView.contentOffset.y < 0) {
self.tableView.contentOffset = CGPointMake(0, 0);
}
}
只有在下拉刷新的时候,切换tab在切回来,刷新头回不去,可以只针对那几个页面来做个处理
网友评论