本人iOS菜鸟一枚, 不太会写文章, 语文从来没及格过, 进入正题
需求分析
看图说话:
一个个输入号码太麻烦了 所以就赋值粘贴了
主要的功能
- 当刚进来的时候有一个空的输入框
- 输入一个号码为正确的号码时再添加一个空的输入框(输入错误不添加给提示)
- 删除号码的功能
做了个简易的demo
判断思路
整个页面上的控件就不用多说了 就是一个UICollectionView
基本上都是采用Block传值的, cell里注册了个通知, 当前的textFeild的内容改变就会收到这个通知, 一大部分逻辑在这里, 直接上代码
- (void)textFeildDidChange:(NSNotification *)noti{
if (noti.object == self.textFeild){
if ([self isMobile:self.textFeild.text]) {
self.item.isNumber = YES;
} else {
self.item.isNumber = NO;
}
self.item.number = self.textFeild.text;
if (self.textFeildChangeBlock) {
self.textFeildChangeBlock(self, self.item);
}
if (self.textFeild.text.length == 0){
if (self.textFeildEmptyString) {
self.textFeildEmptyString(self, self.item);
}
}
if (self.textFeild.text.length > 10) {
self.textFeild.text = [self.textFeild.text substringToIndex:11];
if (![self isMobile:self.textFeild.text]) {
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入正确的手机号" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
}
}
if ([self isMobile:self.textFeild.text]) {
self.textFeild.backgroundColor = RGBColor(242, 245, 235);
self.textFeild.textColor = RGBColor(0, 0, 0);
if (self.nextNumberTextFeildBlock) {
self.nextNumberTextFeildBlock();
}
} else {
self.textFeild.backgroundColor = [UIColor whiteColor];
self.textFeild.textColor = RGBColor(252, 77, 48);
}
}
}
输入的号码每改变一次, 就会调用这个方法, 先是判断是否是手机号, 将输入框的手机号赋值给当前cell绑定的Item上 然后传出去 更新collectionView里的数组的对应的手机号码
手机号不能大于11位, 所以做了一个截取的操作, 当是11为的时候, 判断是不是手机号, 不是的话, 给一个提示. 再一次判断是否是手机号是为了能添加下一个输入框, 让是手机号的cell高亮, 字体颜色高亮
中间有一个判断长度是否为0的操作 是删除空的cell的
下面我们看看这些Block操作的代码
__weak typeof(self)weakSelf = self;
[cell setTextFeildChangeBlock:^(NumberCollectionCell *cell, NumberItem *item) {
NSIndexPath *indexPath2 = [collectionView indexPathForCell:cell];
[weakSelf.numbers replaceObjectAtIndex:indexPath2.row withObject:item];
}];
[cell setNextNumberTextFeildBlock:^{
NumberItem *item = [weakSelf.numbers lastObject];
NSString *number = item.number;
if ([weakSelf isMobile:number]) {
[weakSelf.numbers addObject:[NumberItem numberItemWithNumber:@"" isNumber:NO]];
NSIndexPath *indexPath1 = [NSIndexPath indexPathForItem:weakSelf.numbers.count - 1 inSection:0];
[collectionView insertItemsAtIndexPaths:@[indexPath1]];
NSInteger count = weakSelf.numbers.count;
NSInteger cloum = weakSelf.cloum;
if ((count - 1) % cloum == 0) {
NSInteger lines = count % cloum == 0 ? count / cloum : count / cloum + 1;
CGPoint offset = CGPointMake(0, lines * (weakSelf.itemHeight + 10) + 10);
CGFloat height = self.bounds.size.height;
if (offset.y > height) {
offset.y = offset.y - height;
offset.y = offset.y + 10;
collectionView.contentOffset = offset;
}
}
NumberCollectionCell *cell1 = (NumberCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath1];
[cell1 textFeildBecomeFirstResponder];
}
}];
[cell setTextFeildEmptyString:^(NumberCollectionCell *cell, NumberItem *item) {
NSIndexPath *indexPath = [weakSelf indexPathForCell:cell];
if (indexPath.row != weakSelf.numbers.count - 1) {
NSIndexPath *indexPath1 = [NSIndexPath indexPathForItem:self.numbers.count -1 inSection:0];
NumberCollectionCell *cell1 = (NumberCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath1];
[cell1 textFeildBecomeFirstResponder];
[weakSelf.numbers removeObjectAtIndex:indexPath.row];
[weakSelf deleteItemsAtIndexPaths:@[indexPath]];
}
}];
注意点
- 添加的时候之所以没有给动画, 是因为要让即将出现的cell的textField成为第一响应者, 加了动画有一个时间差, 拿不到这个cell
没有一点注释, 有需要可以私信我
第一次发文章, 不会分享, 将就看一下吧, 有什么问题可以评论, 当然也可以加我QQ互相交流847881570 备注:简书