news 2026/7/23 15:35:53

【iOS】3G-Share仿写总结

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【iOS】3G-Share仿写总结

【iOS】3G-Share仿写总结

文章目录

  • 【iOS】3G-Share仿写总结
  • 登录和注册页面
    • 首页
    • 搜索页面
    • 上传页面
    • 文章和活动页面
    • 个人页面

登录和注册页面

对于登录页面主要是用NSUserDefaults来存储注册信息,然后登录判断是否正确。在邮箱以及账号密码的输入中,我们需要用到谓词以及正则表达式来限制,具体可见我的博客:谓词与正则表达式

-(void)registersuccess{NSString*account=self.accountView.textField.text;NSString*password=self.cipherView.textField.text;if([account isEqualToString:@""]||[password isEqualToString:@""]){UIAlertController*alert=[UIAlertController alertControllerWithTitle:@"注册失败"message:@"账号或密码为空"preferredStyle:UIAlertControllerStyleAlert];UIAlertAction*confirmAction=[UIAlertAction actionWithTitle:@"确认"style:UIAlertActionStyleDefault handler:nil];[alert addAction:confirmAction];[selfpresentViewController:alert animated:YES completion:nil];return;}NSString*email=self.emailView.textField.text;NSString*pattern=@"[A-Z0-9a-z]+@[A-Za-z0-9]+\\.[A-Za-z]{2,}";NSPredicate*predicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];if(![predicate evaluateWithObject:email]){UIAlertController*alert=[UIAlertController alertControllerWithTitle:@"注册失败"message:@"邮箱格式不正确"preferredStyle:UIAlertControllerStyleAlert];UIAlertAction*action=[UIAlertAction actionWithTitle:@"确定"style:UIAlertActionStyleDefault handler:nil];[alert addAction:action];[selfpresentViewController:alert animated:YES completion:nil];return;}NSDictionary*user=@{@"account":account,@"password":password};NSUserDefaults*defaults=[NSUserDefaults standardUserDefaults];// 先取出原来的所有用户NSMutableArray*users=[[defaults objectForKey:@"registeredUsers"]mutableCopy];// 第一次注册时数组不存在if(!users){users=[NSMutableArray array];}for(NSDictionary*dictinusers){if([dict[@"account"]isEqualToString:account]){UIAlertController*alert=[UIAlertController alertControllerWithTitle:@"注册失败"message:@"账号已存在"preferredStyle:UIAlertControllerStyleAlert];[alert addAction:[UIAlertAction actionWithTitle:@"确定"style:UIAlertActionStyleDefault handler:nil]];[selfpresentViewController:alert animated:YES completion:nil];return;}}// 添加新用户[users addObject:user];// 再存回去[defaults setObject:users forKey:@"registeredUsers"];[self.navigationController popViewControllerAnimated:YES];}-(BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string{NSCharacterSet*charSet=[[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@."]invertedSet];//取不包含这些的字符集合,用于过滤NSString*filteredStr=[[string componentsSeparatedByCharactersInSet:charSet]componentsJoinedByString:@""];//遇到不合法的就切割,最后拼接if(range.length==1&&string.length==0){//让到限定字数时也能删除字符returnYES;}elseif(textField.text.length>=15){textField.text=[textField.text substringToIndex:15];returnNO;}elseif([string isEqualToString:filteredStr]&&textField.text.length<=15){returnYES;}returnNO;}

首页

对于首页的假日cell,主要用到了一个双向传值也就是love按钮是否被点击,对于正向采用的是属性传值,对于反向则是用了一个block传值代码如下

if(indexPath.row==0&&indexPath.section==0){myhomeViewController*myhome=[[myhomeViewController alloc]init];myhome.loveSelected=self.firstCellLoveSelected;myhome.loveCount=self.firstCellLoveCount;__weaktypeof(self)weakSelf=self;myhome.loveChangedBlock=^(BOOL selected,NSInteger count){weakSelf.firstCellLoveSelected=selected;weakSelf.firstCellLoveCount=count;NSIndexPath*firstIndexPath=[NSIndexPath indexPathForRow:0inSection:0];[weakSelf.tableView reloadRowsAtIndexPaths:@[firstIndexPath]withRowAnimation:UITableViewRowAnimationNone];};[self.navigationController pushViewController:myhome animated:YES];}}-(void)touch{self.loveSelected=!self.loveSelected;if(self.loveSelected){self.loveCount+=1;}else{self.loveCount-=1;}[selfupdateLoveButton];if(self.loveChangedBlock){self.loveChangedBlock(self.loveSelected,self.loveCount);}}

在这里插入图片描述

搜索页面

在这里主要用了一个searchBar搜索,下面是一个collection实现卡片布局,此处比较简单,不多赘述

上传页面

在这里有几个难点,首先就是这个选择图片,你有两种实现方法:第一是在这个view上加手势,然后跳转照片墙页面,然后选择几个照片将照片回调,第二个就是用苹果自带的PHpicker,在此处由于练习我选择了第一种方法

第二个难点则是这个折叠cell,在此处我是将选中的放在第一个cell,下面的四个cell是不动的,这样就只用控制展开还是折叠以及被选中的label

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{returnself.isExpand?self.titles.count+1:1;}-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];if(!cell){cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];}cell.selectionStyle=UITableViewCellSelectionStyleNone;cell.textLabel.font=[UIFont systemFontOfSize:15];cell.textLabel.textColor=[UIColor blackColor];if(indexPath.row==0){cell.textLabel.text=self.titles[self.selectedIndex];UIImageView*arrowView=[[UIImageView alloc]initWithImage:[UIImage systemImageNamed:self.isExpand?@"chevron.up":@"chevron.down"]];arrowView.tintColor=[UIColor blackColor];cell.accessoryView=arrowView;}else{cell.textLabel.text=self.titles[indexPath.row-1];cell.accessoryView=nil;}returncell;}-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{[self.view bringSubviewToFront:self.tableView];if(indexPath.row==0){self.isExpand=!self.isExpand;}else{self.selectedIndex=indexPath.row-1;self.isExpand=NO;}CGFloat rowHeight=40;NSInteger rowCount=self.isExpand?self.titles.count+1:1;[self.tableView mas_updateConstraints:^(MASConstraintMaker*make){make.height.mas_equalTo(rowHeight*rowCount);}];[self.tableView reloadData];}

在此处还有一个textView没有placeHolder的问题,此时我们需要用一个灰色的label添加在textView中,并用textView的代理方法监视text,一旦text不为空就将灰色label设为hidden

-(void)textViewDidChange:(UITextView*)textView{self.placeholderLabel.hidden=textView.text.length>0;}

文章和活动页面

在这里就是一个简单的分栏控件以及scrollView搭配以及TableView的使用

个人页面

在此处的页面比较多,我就只说一些难点地方

首先就是新关注页面,由于不能每次打开页面都是一种关注状态,所以我们需要用NSUserDefaults存储关注信息

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{followTableViewCell*followcell=[tableView dequeueReusableCellWithIdentifier:@"cell"forIndexPath:indexPath];followcell.selectionStyle=UITableViewCellSelectionStyleNone;followcell.avatar.image=[UIImage imageNamed:self.imageArray[indexPath.row]];followcell.name.text=self.nameArray[indexPath.row];followcell.isFollow=[self.isFollowArray[indexPath.row]boolValue];__weaktypeof(self)weakSelf=self;followcell.followStateChanged=^(BOOL isFollow){weakSelf.isFollowArray[indexPath.row]=@(isFollow);[[NSUserDefaults standardUserDefaults]setObject:weakSelf.isFollowArray forKey:@"followStateArray"];[weakSelf.tableview reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationNone];};returnfollowcell;}-(void)touch{self.isFollow=!self.isFollow;if(self.followStateChanged){self.followStateChanged(self.isFollow);}}

其次就是私信页面,私信页面需要隐藏TabBar

// 隐藏tabBar-(void)viewWillAppear:(BOOL)animated{[superviewWillAppear:animated];self.tabBarController.tabBar.hidden=YES;}// 恢复tabBar-(void)viewWillDisappear:(BOOL)animated{[superviewWillDisappear:animated];self.tabBarController.tabBar.hidden=NO;}

实现消息的交替发送

-(void)sendClick{if(self.textField.text.length==0){return;}ChatModel*model=[[ChatModel alloc]init];model.content=self.textField.text;if(self.isSelf){model.type=MessageTypeSelf;}else{model.type=MessageTypeOther;}[self.dataArray addObject:model];self.isSelf=!self.isSelf;NSIndexPath*indexPath=[NSIndexPath indexPathForRow:self.dataArray.count-1inSection:0];[self.tableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationBottom];[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];self.textField.text=@"";}

将最后一句话回传给消息列表界面,保证列表显示的是聊天的最后一句话

-(void)backClick{if(self.messageBlock&&self.dataArray.count){ChatModel*last=self.dataArray.lastObject;self.messageBlock(last.content);}[self.navigationController popViewControllerAnimated:YES];}

在基本资料页面,由于一开始我没有设置可编辑状态,后面我用的是UIAlertController来修改资料,以及用PHPicker来选择头像

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{if(indexPath.row==0){PHPickerConfiguration*config=[[PHPickerConfiguration alloc]init];config.selectionLimit=1;//只支持单选config.filter=[PHPickerFilter imagesFilter];// 只显示图片PHPickerViewController*picker=[[PHPickerViewController alloc]initWithConfiguration:config];picker.delegate=self;[selfpresentViewController:picker animated:YES completion:nil];}if(indexPath.row==1){UIAlertController*alert=[UIAlertController alertControllerWithTitle:@"修改昵称"message:nil preferredStyle:UIAlertControllerStyleAlert];[alert addTextFieldWithConfigurationHandler:^(UITextField*_Nonnull textField){textField.text=self.nameText;}];UIAlertAction*confirm=[UIAlertAction actionWithTitle:@"确定"style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnull action){self.nameText=alert.textFields.firstObject.text;[selfsaveUserInfo];[self.tableview reloadData];}];[alert addAction:confirm];[alert addAction:[UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:nil]];[selfpresentViewController:alert animated:YES completion:nil];}if(indexPath.row==2){UIAlertController*alert=[UIAlertController alertControllerWithTitle:@"修改签名"message:nil preferredStyle:UIAlertControllerStyleAlert];[alert addTextFieldWithConfigurationHandler:^(UITextField*_Nonnull textField){textField.text=self.sentenceText;}];UIAlertAction*confirm=[UIAlertAction actionWithTitle:@"确定"style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnull action){self.sentenceText=alert.textFields.firstObject.text;[selfsaveUserInfo];[self.tableview reloadData];}];[alert addAction:confirm];[alert addAction:[UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:nil]];[selfpresentViewController:alert animated:YES completion:nil];}if(indexPath.row==4){UIAlertController*alert=[UIAlertController alertControllerWithTitle:@"修改邮箱"message:nil preferredStyle:UIAlertControllerStyleAlert];[alert addTextFieldWithConfigurationHandler:^(UITextField*_Nonnull textField){textField.text=self.emailText;}];UIAlertAction*confirm=[UIAlertAction actionWithTitle:@"确定"style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnull action){self.emailText=alert.textFields.firstObject.text;[selfsaveUserInfo];[self.tableview reloadData];}];[alert addAction:confirm];[alert addAction:[UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:nil]];[selfpresentViewController:alert animated:YES completion:nil];}if(indexPath.row==3){UIAlertController*alert=[UIAlertController alertControllerWithTitle:@"选择性别"message:nil preferredStyle:UIAlertControllerStyleActionSheet];UIAlertAction*male=[UIAlertAction actionWithTitle:@"男"style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnull action){self.isMale=YES;[selfsaveUserInfo];[self.tableview reloadData];}];UIAlertAction*female=[UIAlertAction actionWithTitle:@"女"style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnull action){self.isMale=NO;[selfsaveUserInfo];[self.tableview reloadData];}];[alert addAction:male];[alert addAction:female];[alert addAction:[UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:nil]];[selfpresentViewController:alert animated:YES completion:nil];}}

在这里插入图片描述

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/23 15:35:50

DS92LV8028串行器芯片:8通道LVDS高速传输与内置BIST测试实战解析

1. 项目概述与核心价值 在视频采集卡、工业相机或者高端显示系统的硬件设计里&#xff0c;工程师们最头疼的问题之一&#xff0c;就是如何把板卡上FPGA或处理器产生的大量并行数据&#xff0c;干净利落地送到另一块板卡上。传统的并行总线&#xff0c;动辄几十根线&#xff0c;…

作者头像 李华
网站建设 2026/7/23 15:32:55

迅雷盘获取直链解析,在线操作就能满速下载

当我们想要下载迅雷网盘里面的一些文件时&#xff0c;&#xff0c;当你不想开会员又想下载速度很快的时候&#xff0c;不妨来看看我这方法&#xff1a;点我打开方法 下载速度看你的网速和宽带跑个10几M/秒不是问题&#xff0c;亲测有效&#xff0c;接下来就是教程部分 打开上面…

作者头像 李华
网站建设 2026/7/23 15:32:25

UTM虚拟机:跨架构虚拟化技术与移动生产力实践

1. 为什么需要UTM虚拟机&#xff1f;在移动设备上运行完整的桌面操作系统&#xff0c;这个需求听起来像是极客的专属玩具&#xff0c;但实际应用场景远超想象。作为一名在虚拟化领域踩坑多年的开发者&#xff0c;我亲身体验过在iPad Pro上编译Linux内核、在M1 Mac上测试Windows…

作者头像 李华
网站建设 2026/7/23 15:30:32

涨薪技术|项目构建工具Maven使用教程

01认识Maven如今我们构建一个项目需要用到很多第三方的类库&#xff0c;如写一个使用Spring的Web项目就需要引入大量的jar包。一个项目Jar包的数量之多往往让我们瞠目结舌&#xff0c;并且Jar包之间的关系错综复杂&#xff0c;一个Jar包往往又会引用其他Jar包&#xff0c;缺少任…

作者头像 李华
网站建设 2026/7/23 15:29:33

079、STM32Cube.AI的异常检测案例

079 STM32Cube.AI的异常检测案例 昨晚调试到凌晨三点,板子上的风扇突然不转了——不是硬件烧了,是AI模型把正常工况判成了异常。这种误报在工业现场比模型精度低更让人头疼。今天聊聊STM32Cube.AI做异常检测时那些文档里不会写的坑。 从一次现场故障说起 客户反馈说设备在…

作者头像 李华
网站建设 2026/7/23 15:29:31

080、STM32Cube.AI的预测维护案例

080 STM32Cube.AI的预测维护案例 从一次产线停机说起 去年帮一家电机厂做预测维护方案,现场调试时遇到一个诡异现象:模型在开发板上推理结果完全正确,一挂到产线振动传感器上,连续三天误报率飙到40%。排查到最后,发现是Cube.AI生成的代码里,输入数据的归一化参数写死了…

作者头像 李华