昨天去一家公司面試,面試官問了我在項目開發中遇到過哪些問題,是什么引起的,怎樣解決的? 當時由於有點小緊張只說出了一兩點,現在就來好好總結一下.
問題:
1.兩表聯動
所謂的兩表聯動就是有左右兩個表格,左邊的表格由二個分類構成,大分類用HeaderView 展示,小分類用cell 展示;右邊的表格負責展示分類下的商品. 通過左邊的分類點擊展示對應右邊表格的商品好處理,通過tableView的didSelectRowAtIndexPath 方法就能解決,可關鍵是滑動右邊的表格要對應選中左邊的分類,怎么處理???
還是tableView 的代理方法
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section { if (_isRelate) { NSInteger topCellSection = [[[tableView indexPathsForVisibleRows] firstObject] section]; //右邊的section } } - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { if (_isRelate) { NSInteger topCellSection = [[[tableView indexPathsForVisibleRows] firstObject] section]; //右邊的section if (tableView == _rightTableView) { //根據右邊表格的section 查找左邊對應的row 或section } } } #pragma mark - tableView 是繼承scrollView 的 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { _isRelate = YES; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if (tableView.tag==0) return 40; else return 0.01; } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ if (tableView.tag==0) return 0; else return 0.01; }
注:右邊滑動表格的 heightForHeaderInSection 以及 heightForFooterInSection 必須>0 使用才有效果
這是參考的gitHub 上的代碼實現的,如需看源碼,去gitHub搜索兩表聯動即可. 如果有更好的處理辦法,歡迎補充!
2.關於UITabBar的隱藏
剛開始一直是使用 [self.tabBarController.tabBar setHidden:NO]; 對tabBar 進行隱藏的,這個代碼在一般情況確實能實現隱藏的效果,但是什么情況下會有問題呢?
當控件(比如button)靠近屏幕底部的時候,無法觸發事件,這個問題糾結了好久,也請教了許多前輩,后來我也上網差了些資料,終於,找到原因了. 就是tabBar 的隱藏問題導致的.雖然tabBar欄被隱藏了,但在隱藏的區域會成為一片空白區,無法被其他視圖使用。這也就是為何button不響應時間的原因了.
解決辦法:self.hidesBottomBarWhenPushed = YES; (但是要注意使用的時機,應該在視圖push 前就設置隱藏,在pop 前設置為NO)
3.關於鍵盤遮擋輸入框的問題
這個問題有兩種情況,1種是輸入框在self.view 上 另外一種是輸入框在self.tableView 上.
如果是第一種情況,可以去看一下我第一篇博 http://www.cnblogs.com/Cyan-zoey/p/5133167.html
如果是第二種情況:
_oY=_rightTableView.frame.size.height;//記錄初始化的時候tableView 的高度 //鍵盤出現的時候 - (void)keyboardWillShow:(NSNotification *)notification { CGRect initialFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGRect convertedFrame = [self.view convertRect:initialFrame fromView:nil]; CGRect tvFrame = _rightTableView.frame; tvFrame.size.height = convertedFrame.origin.y; _rightTableView.frame = tvFrame; } //隱藏鍵盤 - (void)keyboardWillHide:(NSNotification *)notification { CGRect tvFrame = _rightTableView.frame; tvFrame.size.height =_oY; _rightTableView.frame = tvFrame; }
推薦使用第三方: IQKeyboardManager
4.手勢沖突
a.UIWebView 嵌套UIScrollView
因為webView的內容是從網絡加載的H5的頁面,放入scrollView 里面造成手勢沖突,從第一個頁面滑動到webView所在頁面的時候,就無法滑回去了,試了很多種辦法都沒解決. 后來去掉了UIScrollView. 如果有解決辦法的,望賜教! (后來試了下webView中放其他的網頁,發現並沒有產生這個問題,所以可能是嵌入的網頁做了什么處理,導致出現了這個情況)
b.tableview上添加點按手勢與cell 點擊事件沖突。
我在tableView 上添加了點按手勢,在方法中刪除了彈出層
[_tableView setUserInteractionEnabled:YES]; UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tableTap)]; tap.delegate=self; [_tableView addGestureRecognizer:tap]; #pragma mark ---點擊tableView 中的空白部分刪除彈出層 - (void)tableTap{ [_typeView removeFromSuperview]; _isEdit=NO; //在彈出層顯示的時候設置為Yes ,消失的時候設置為NO } 解決辦法:實現手勢的代理方法 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { // 輸出點擊的view的類名 //NSLog(@"%@", NSStringFromClass([touch.view class])); if (_isEdit==YES) { //不攔截 if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) { return YES; } }else{ if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) { return NO; } } // 若為UITableViewCellContentView(即點擊了tableViewCell),則不截獲Touch事件 return YES; }
c.scrollView上添加手勢不響應
http://blog.csdn.net/zouxianm/article/details/48194657
補充:事件沖突
self.view 上添加tableView ,在self.view 整體往上移動的時候,tableView 的行點擊事件失效,任何手勢都不響應。
解決辦法:將tableView 添加到到window 上。
5.彈出層與刪除層
5.1 彈出可移動的view(類似於windows上面可移動的彈框)
//先創建手勢 UIPanGestureRecognizer * panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(doHandlePanAction:)]; [self.bigView addGestureRecognizer:panGestureRecognizer]; //拖動處理 - (void) doHandlePanAction:(UIPanGestureRecognizer *)paramSender{ CGPoint point = [paramSender translationInView:self.view]; NSLog(@"X:%f;Y:%f",point.x,point.y); paramSender.view.center = CGPointMake(paramSender.view.center.x + point.x, paramSender.view.center.y + point.y); [paramSender setTranslation:CGPointMake(0, 0) inView:self.view]; }
5.2 點擊彈出層之外的地方,刪除彈出層。
a. 彈出層加在self.view 上面
#pragma mark ---觸摸事件---判斷當前點是否在某個范圍內
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint t=[[touches anyObject] locationInView:self.bigView]; //bigView為彈出層
if (!CGRectContainsPoint(_bigView.frame, point)) //不在某個范圍內
{
[self.bigView removeFromSuperview];//刪除
}
}
b.彈出層加在AppDelegate的window上面。
//先注冊點擊手勢
[_bigView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didmissTK:)];
[_bigView addGestureRecognizer:tap];
//實現方法
- (void)didmissTK:(UIGestureRecognizer *)gest{
CGPoint point = [gest locationInView:_bigView];
if (!CGRectContainsPoint(_bigView.frame, point, point)){
[self.bigView removeFromSuperview];
}
}
原因: 加在self.view 上面視圖的不包括頂部的導航欄,要想包含導航欄就只能加在window 上面。 加在window上面就相當於在self.view 上覆蓋了一層,因此 touchesBegan方法就無法響應,因此只能在window的彈出層上加手勢去刪除彈出層。
6.點擊按鈕不響應
a. 看一下該按鈕是否是在UIImageView 上面,如果在,看一下是否把交互打開[_bigView setUserInteractionEnabled:YES];
b. 打印一下按鈕的frame以及按鈕父視圖的frame,看一下按鈕是否在父視圖的范圍內,如果不在,那么按鈕不響應。在父視圖的frame 內不響應,查看frame 是否不在屏幕內(self.view ),如果父視圖的self.view.frame.original 為負,不響應事件。
c. 看一下按鈕所在視圖之上是否有覆蓋層。如果有,移除覆蓋層即可。
還有就是如果給按鈕設置圓角或者邊框顏色(與layer有關的)沒有反應,記得設置 [btn.layer setMasksToBounds:YES];
7. Cell背景色覆蓋按鈕的顏色
cell默認選中行的顏色是灰色的,如果cell上有按鈕等控件,會讓自身的顏色被灰色覆蓋
解決方法: 在自定義的cell中實現如下兩個方法即可:
-(void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
被覆蓋的控件名.backgroundColor = UIColor.redColor;
}
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
被覆蓋的控件名.backgroundColor = UIColor.redColor;
}
暫時只能想到這么多啦,再想到了新的會及時補充的.
現在來總結一下項目中使用到的SDK
1.騰訊地圖SDK :進行地位,以及地圖展示
2.SDWebImage:圖片處理
實現過程:a: 如果設置了placeholderImage(占位圖)就先展示占位圖
b: SDImageCache 從內存中查找緩存的圖片-》 找到了圖片 , SDImageCacheDelegate 回調 到UIImageView+WebCache 等前端展示圖片
-》 沒找到圖片 則去硬盤中查找-》找到了,SDImageCacheDelegate回調展示圖片
-》沒找到 ,使用SDWebImageManager下載圖片
c:替換占位圖
3.MJRefersh:上下拉刷新
4.MBProgressHUD:提示框
5.AFNetWorking: 網絡請求
(1)組成:(封裝自NSURLSession)
NSURLSession (AFURLSessionManager/AFHTTPSessionManager)----網絡通信模塊(核心模塊)
Security(AFSecurityPolicy)----網絡通訊安全策略模塊
Reachability(AFNetworkReachabilityManager)----網絡狀態監聽模塊
Serialization(AFURLResponseSerializationAFURLRequestSerialization/)---網絡通信信息序列化、反序列化模塊
UIKit ----UIKit庫
6.友盟應用統計錯誤分析
7.ping++:第三方支付
8.ZBarSDK:掃二維碼/條形碼 詳細請見:http://www.cnblogs.com/Cyan-zoey/p/6121909.html
9.GtSdk :消息推送 詳情請見:http://www.cnblogs.com/Cyan-zoey/archive/2016/04/28.html
10.融雲即時通訊,集成客服.