【iOS】3G-Share仿写总结 【iOS】3G-Share仿写总结文章目录【iOS】3G-Share仿写总结登录和注册页面首页搜索页面上传页面文章和活动页面个人页面登录和注册页面对于登录页面主要是用NSUserDefaults来存储注册信息然后登录判断是否正确。在邮箱以及账号密码的输入中我们需要用到谓词以及正则表达式来限制具体可见我的博客谓词与正则表达式-(void)registersuccess{NSString*accountself.accountView.textField.text;NSString*passwordself.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*emailself.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.length1string.length0){//让到限定字数时也能删除字符returnYES;}elseif(textField.text.length15){textField.text[textField.text substringToIndex:15];returnNO;}elseif([string isEqualToString:filteredStr]textField.text.length15){returnYES;}returnNO;}首页对于首页的假日cell主要用到了一个双向传值也就是love按钮是否被点击对于正向采用的是属性传值对于反向则是用了一个block传值代码如下if(indexPath.row0indexPath.section0){myhomeViewController*myhome[[myhomeViewController alloc]init];myhome.loveSelectedself.firstCellLoveSelected;myhome.loveCountself.firstCellLoveCount;__weaktypeof(self)weakSelfself;myhome.loveChangedBlock^(BOOL selected,NSInteger count){weakSelf.firstCellLoveSelectedselected;weakSelf.firstCellLoveCountcount;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.loveCount1;}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.count1:1;}-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{UITableViewCell*cell[tableView dequeueReusableCellWithIdentifier:cell];if(!cell){cell[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell];}cell.selectionStyleUITableViewCellSelectionStyleNone;cell.textLabel.font[UIFont systemFontOfSize:15];cell.textLabel.textColor[UIColor blackColor];if(indexPath.row0){cell.textLabel.textself.titles[self.selectedIndex];UIImageView*arrowView[[UIImageView alloc]initWithImage:[UIImage systemImageNamed:self.isExpand?chevron.up:chevron.down]];arrowView.tintColor[UIColor blackColor];cell.accessoryViewarrowView;}else{cell.textLabel.textself.titles[indexPath.row-1];cell.accessoryViewnil;}returncell;}-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{[self.view bringSubviewToFront:self.tableView];if(indexPath.row0){self.isExpand!self.isExpand;}else{self.selectedIndexindexPath.row-1;self.isExpandNO;}CGFloat rowHeight40;NSInteger rowCountself.isExpand?self.titles.count1: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.hiddentextView.text.length0;}文章和活动页面在这里就是一个简单的分栏控件以及scrollView搭配以及TableView的使用个人页面在此处的页面比较多我就只说一些难点地方首先就是新关注页面由于不能每次打开页面都是一种关注状态所以我们需要用NSUserDefaults存储关注信息-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{followTableViewCell*followcell[tableView dequeueReusableCellWithIdentifier:cellforIndexPath:indexPath];followcell.selectionStyleUITableViewCellSelectionStyleNone;followcell.avatar.image[UIImage imageNamed:self.imageArray[indexPath.row]];followcell.name.textself.nameArray[indexPath.row];followcell.isFollow[self.isFollowArray[indexPath.row]boolValue];__weaktypeof(self)weakSelfself;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.hiddenYES;}// 恢复tabBar-(void)viewWillDisappear:(BOOL)animated{[superviewWillDisappear:animated];self.tabBarController.tabBar.hiddenNO;}实现消息的交替发送-(void)sendClick{if(self.textField.text.length0){return;}ChatModel*model[[ChatModel alloc]init];model.contentself.textField.text;if(self.isSelf){model.typeMessageTypeSelf;}else{model.typeMessageTypeOther;}[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.messageBlockself.dataArray.count){ChatModel*lastself.dataArray.lastObject;self.messageBlock(last.content);}[self.navigationController popViewControllerAnimated:YES];}在基本资料页面由于一开始我没有设置可编辑状态后面我用的是UIAlertController来修改资料以及用PHPicker来选择头像-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{if(indexPath.row0){PHPickerConfiguration*config[[PHPickerConfiguration alloc]init];config.selectionLimit1;//只支持单选config.filter[PHPickerFilter imagesFilter];// 只显示图片PHPickerViewController*picker[[PHPickerViewController alloc]initWithConfiguration:config];picker.delegateself;[selfpresentViewController:picker animated:YES completion:nil];}if(indexPath.row1){UIAlertController*alert[UIAlertController alertControllerWithTitle:修改昵称message:nil preferredStyle:UIAlertControllerStyleAlert];[alert addTextFieldWithConfigurationHandler:^(UITextField*_Nonnull textField){textField.textself.nameText;}];UIAlertAction*confirm[UIAlertAction actionWithTitle:确定style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnull action){self.nameTextalert.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.row2){UIAlertController*alert[UIAlertController alertControllerWithTitle:修改签名message:nil preferredStyle:UIAlertControllerStyleAlert];[alert addTextFieldWithConfigurationHandler:^(UITextField*_Nonnull textField){textField.textself.sentenceText;}];UIAlertAction*confirm[UIAlertAction actionWithTitle:确定style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnull action){self.sentenceTextalert.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.row4){UIAlertController*alert[UIAlertController alertControllerWithTitle:修改邮箱message:nil preferredStyle:UIAlertControllerStyleAlert];[alert addTextFieldWithConfigurationHandler:^(UITextField*_Nonnull textField){textField.textself.emailText;}];UIAlertAction*confirm[UIAlertAction actionWithTitle:确定style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnull action){self.emailTextalert.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.row3){UIAlertController*alert[UIAlertController alertControllerWithTitle:选择性别message:nil preferredStyle:UIAlertControllerStyleActionSheet];UIAlertAction*male[UIAlertAction actionWithTitle:男style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnull action){self.isMaleYES;[selfsaveUserInfo];[self.tableview reloadData];}];UIAlertAction*female[UIAlertAction actionWithTitle:女style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnull action){self.isMaleNO;[selfsaveUserInfo];[self.tableview reloadData];}];[alert addAction:male];[alert addAction:female];[alert addAction:[UIAlertAction actionWithTitle:取消style:UIAlertActionStyleCancel handler:nil]];[selfpresentViewController:alert animated:YES completion:nil];}}在这里插入图片描述