ios 中tableview和scrollView的區別

來源:互聯網
上載者:User

標籤:des   c   style   class   blog   a   

scrollView:

1. 介紹scrollView一些屬性      

 1>.要想使用scrollView必須做兩件事       

     1).設定scrollView內容        

    2).設定contentSize (滾動範圍)  

 2>.其他屬性            

   1). contentOffset(滾動位置)           

    2). contentInset(額外增加的捲動區域)           

    3). bounces (設定UIScrollView是否需要彈簧效果)    

      4). crollEnabled (設定UIScrollView是否能滾動)    

      5). showsHorizontalScrollIndicator (是否顯示水平捲軸)   

   6). showsVerticalScrollIndicator (是否顯示垂直捲軸)

2. 代理      

 1>代理思想兩個思想           

  1).監聽思想:B監聽A發生了什麼事情            

    2).通知思想:A發生了一些事情,要通知B去做   

    2>scrollView的代理使用            

     1).如何成為代理(三步)                

   *聲明協議                   *設定代理對象self.scrollView.delegate = self;       *實現協議方法

       2).代理監聽scrollView的拖拽事件

// 開始拖拽 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;             // 結束拖拽 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;             // scrollView滾動時執行 - (void)scrollViewDidScroll:(UIScrollView *)scrollView

 3).用代理實現縮放

*成為UIScrollView的代理() *設定縮放對象(通過viewForZoomingInScrollView方法) *設定縮放為範圍(maximumZoomScale、minimumZoomScale)

3. 定時器建立兩種方式

1>. self.timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(方法) userInfo:nil repeats:YES];   當另一個scrollView運行時,會停止定時器的scrollView,只能執行一個scrollView.    

2>. self.timer = [NSTimer timerWithTimeInterval:1.f target:self selector:@selector(方法) userInfo:nil repeats:YES];   [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

4. 自訂協議並使用

1>.定義協議(三步)        

  *定義protocol(兩種optional[代理對象可不實現]、required[代理對象必須實現])        

  *增加代理屬性(weak) @property (weak, nonatomic) id<LFAppInfoViewDelegate> delegate;  

    *給代理髮訊息,調用代理的方法(需要判斷代理對象是否實現了該方法,不判斷調用後(編譯時間不會)會報錯)   注意:定義協議的名稱命名[類名+Delegate]、協議方法的命名規範[方法名稱需要去掉首碼,並且將自己作為參數]

  2>.使用代理(三步)   

  *聲明協議            

    *設定代理對象            

    *實現協議方法(本例是在代理對象[控制器] 添加一個UILabel)

tableView:

1. UITableView 需要設定資料來源才能顯示資料       

     1>.會向資料來源查詢一共多少組,每組多少行,每行顯示什麼資料    

  2>.資料來源必須遵守UITableViewDateSource協議    

  3> 一共有多少組        

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{}   第section組有多少行        

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{}   每一行顯示什麼內容        

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}   第section組頭部顯示什麼標題                   - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{}   第section組底部顯示什麼標題          

    - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{}   當每一行的cell的高度不一致的時候就使用代理方法設定cell的高度           

      - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{}   當每一行的cell高度一致的時候使用屬性設定cell的高度           

self.tableView.rowHeight = 60;   可以最佳化記憶體的可變數組定義                

  NSMutableArray *models = [NSMutableArray arrayWithCapacity : (NSUInteger)]

2. cell常見屬性       

1>.cell.textLabel.text 標題       

2>.cell.detailTextLabel.text 介紹       

3>.cell.imageView.image 圖片      

4>.cell.accessoryView 輔助視圖       

5>.cell.accessoryView 自訂輔助視圖       

6>.cell.backgroundView  設定cell的背景顏色             

1).通過backgroundColor 和 backgroundView都可以設定cell的背景             

2).但是backgroundView 的優先順序比 backgroundColor的高             

3).所以如果同時設定了backgroundColor和backgroundView, backgroundView會蓋住backgroundColor  

7>.cell.selectedBackgroundView 設定選中狀態的背景

3. UITableView常見屬性       

1>. tableview.separatorStyle 設定分割線樣式       

2>. tableview.separatorColor 設定分割線顏色             自訂色彩            

[UIColor colorWithRed:色值/255.f green:色值/255.f blue:色值/255.f alpha:色值/255.f];     

擷取螢幕寬度: [UIScreen mainScreen].bounds.size.width;   

3>. tableview.tableHeaderView 設定tableView的頭部視圖 一般用於放廣告       

4>. tableview.tableFooterView 設定tableView的底部視圖 一般用於放置載入更多按鈕       

5>. [self.tableView reloadData]; 重新整理表格             // 重新整理指定行             

NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];     [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationRight];

4. 最佳化cell的方法       

1>.先去緩衝池中尋找是否有滿足條件的Cell     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];   

2>.如果緩衝池中沒有合格cell,就自己建立一個Cell     if (nil == cell) {       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];     }   

3>.建立Cell, 並且設定一個唯一的標記 : identifier  注 : 定義變數 NSString *identifier 推薦用 static定義靜態局部變數,不推薦用宏.  

 4>.設定cell資料並返回cell

5. tableView代理方法      

 1>. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{} //當某一行被選中的時候調用      

 2>. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{} //當某一行取消選中的時候調用      

 3>. UIAlertView的一些屬性和代理方法             

1). UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"修改資料" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil]; //建立一個彈窗             

2). alert.alertViewStyle = UIAlertViewStyle...; //設定alert的樣式, 讓alert顯示出uitextfield     

3). UITextField *textField =  [alert textFieldAtIndex:0]; //擷取alert中的textfield     

4). [alert show]; //顯示彈窗             

5). - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{} // alertView的按鈕被點擊的時候就會調用

6. 自訂cell兩種方式       

1>. 純程式碼:每個cell子控制項的個數和位置不一樣       

2>. 通過xib: cell一樣且固定的介面             載入xib的方式:       

1). [[[NSBundle mainBundle] loadNibNamed:@"xib名" owner:nil options:nil] firstObject];       

2).UINib *nib = [UINib nibWithNibName:@"xib名" bundle:nil];         UIView *view = [[nib instantiateWithOwner:nil options:nil]firstObject];   3>. 延遲調用             dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{   });   4>.init方法只有通過代碼建立控制項的時候才會調用; awakeFromNib方法在控制項通過xib或者storyboard建立的時候才會調用       

5>. 協議規範             協議名稱 : 控制項名稱 + Delegate     協議方法名稱:控制項名稱去掉首碼 + 含義             在協議方法中將自己(觸發發放的)控制項傳出去的目的是方便用於區分哪個控制項觸發了該方法       

6>. 代碼建立的子控制項,添加到contentView中  [self.contentView addSubview:子控制項];   

7>. 計算文字寬高             CGSize *maxSize = CGSizeMake(300, MAXFLOAT); // 設定文字範圍             NSDictionary *dict = @{NSFontAttributeName : font}; // 字型             // 如果將來計算的文字的範圍超出了指定的範圍,返回的就是指定的範圍             // 如果將來計算的文字的範圍小於指定的範圍, 返回的就是真實的範圍             CGSize size =  [NSString *str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size; // 計算文字寬高       

8>.通過代碼自訂cell的方法             

1).建立一個繼承自UITableViewCell的類             

2).重寫initWithStyle:reuseIdentifier:方法                   添加所有需要顯示的子控制項(不需要設定子控制項的資料和frame,  子控制項要添加到contentView中)       進行子控制項一次性的屬性設定(有些屬性只需要設定一次, 比如字型\固定的圖片)     

3).提供2個模型                   資料模型: 存放文字資料\圖片資料                   frame模型: 存放資料模型\所有子控制項的frame\cell的高度             4).cell擁有一個frame模型(不要直接擁有資料模型)     

5).重寫frame模型屬性的setter方法: 在這個方法中設定子控制項的顯示資料和frame     

6).frame模型資料的初始化已經採取懶載入的方式(每一個cell對應的frame模型資料只載入一次)

7. 通知機制       

1>. 通知中樞(NSNotificationCenter)     每一個應用程式都有一個通知中樞(NSNotificationCenter)執行個體,專門負責協助不同對象之間的訊息通訊             建立通知中樞             NSNotificationCenter *center = [NSNotificationCenter defaultCenter];   

2>. 一個完整的通知一般包含3個屬性:             - (NSString *)name; // 通知的名稱             - (id)object; // 通知發行者(是誰要發布通知)     - (NSDictionary *)userInfo; // 一些額外的資訊(通知發行者傳遞給通知接收者的資訊內容)   

3>. 初始化一個通知(NSNotification)對象             + (instancetype)notificationWithName:(NSString *)aName object:(id)anObject;     + (instancetype)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;     - (instancetype)initWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo;   

4>. 通知中樞(NSNotificationCenter)提供了相應的方法來發布通知             - (void)postNotification:(NSNotification *)notification;  // 發布一個notification通知,可在notification對象中設定通知的名稱、通知發行者、額外資訊等             - (void)postNotificationName:(NSString *)aName object:(id)anObject;  // 發布一個名稱為aName的通知,anObject為這個通知的發行者             - (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;  // 發布一個名稱為aName的通知,anObject為這個通知的發行者,aUserInfo為額外資訊       

5>.註冊通知監聽器(Observer)     - (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;     observer:監聽器,即誰要接收這個通知                 aSelector:收到通知後,回調監聽器的這個方法,並且把通知對象當做參數傳入                 aName:通知的名稱。如果為nil,那麼無論通知的名稱是什麼,監聽器都能收到這個通知                 anObject:通知發行者。如果為anObject和aName都為nil,監聽器都收到所有的通知       

6>. 取消註冊通知監聽器             通知中樞不會保留(retain)監聽器對象,在通知中樞註冊過的對象,必須在該對象釋放前取消註冊。否則,當相應的通知再次出現時,通知中樞仍然會向該監聽器發送訊息。因為相應的監聽器對象已經被釋放了,所以可能會導致應用崩潰             - (void)removeObserver:(id)observer;     - (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;     一般在監聽器銷毀之前取消註冊(如在監聽器中加入下列代碼):             - (void)dealloc {       //[super dealloc];  非ARC中需要調用此句                   [[NSNotificationCenter defaultCenter] removeObserver:self];     }   

7>. 通知和代理的選擇             

1).共同點                   

利用通知和代理都能完成對象之間的通訊             

2).不同點                   

代理 : 一對一關聯性(1個對象只能告訴另1個對象發生了什麼事情)       

通知 : 多對多關係(1個對象能告訴N個對象發生了什麼事情, 1個對象能得知N個對象發生了什麼事情)

8. 鍵盤通知       UIKeyboardWillShowNotification // 鍵盤即將顯示       UIKeyboardDidShowNotification // 鍵盤顯示完畢       UIKeyboardWillHideNotification // 鍵盤即將隱藏       UIKeyboardDidHideNotification // 鍵盤隱藏完畢       UIKeyboardWillChangeFrameNotification // 鍵盤的位置尺寸即將發生改變       UIKeyboardDidChangeFrameNotification // 鍵盤的位置尺寸改變完畢       附帶跟鍵盤有關的額外資訊(字典),字典常見的key如下:   UIKeyboardFrameBeginUserInfoKey // 鍵盤剛開始的frame   UIKeyboardFrameEndUserInfoKey // 鍵盤最終的frame(動畫執行完畢後)   UIKeyboardAnimationDurationUserInfoKey // 鍵盤動畫的時間       UIKeyboardAnimationCurveUserInfoKey // 鍵盤動畫的執行節奏(快慢) 9. 其他       1>. 子控制項不顯示排錯方法              

1).查看是否調用添加的方法              

2).frame為空白(沒有設定frame)     

3).hidden 是否為yes     

4).alpha <=0.1     

5).沒有添加到父控制項中              

6).查看夫控制項有沒有以上幾點         但凡在init方法中擷取到的frame都是0       - (void)layoutSubviews       {         [super layoutSubviews];         // 該方法在控制項的frame被改變的時候就會調用                 // 該方法一般用於調整子控制項的位置           }   2>.     // 已經被添加到父視圖上的時候會調用             - (void)didMoveToSuperview     {     }     // 即將被添加到父視圖上的時候會調用             - (void)willMoveToSuperview:(UIView *)newSuperview     {     }  

 3> UITextField中添加左右視圖             self.textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];     // 設定左邊視圖的顯示模式             self.textField.leftViewMode = UITextFieldViewModeAlways;     self.textField.rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];     // 設定右邊視圖的顯示模式             self.textField.rightViewMode = UITextFieldViewModeAlways;   

4>. // 設定btn中的圖片不填充整個imageview     btn.imageView.contentMode = UIViewContentModeCenter;     // 超出範圍的圖片不要剪下             // btn.imageView.clipsToBounds = NO;     btn.imageView.layer.masksToBounds = NO;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.