iOS ,UITableViewDataSource 和 UITableViewDelegate協議中常用方法

來源:互聯網
上載者:User

標籤:des   style   color   os   io   使用   ar   for   cti   


UITableViewDataSource 協議中常用方法

1.設定右邊索引值

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

2.設定分組標識

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section


3.設定分組個數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

4.設定行數

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

5.建立cell(使用重用機制,如下例)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  1.建立重用標識符
   staticNSString *identifier =@"reuse”;

 2.去重用隊列中根據標識符取可重用的cell 

  AddressBookCell *cell = [tableViewdequeueReusableCellWithIdentifier:identifier];     

3.判斷是否擷取到可重用的cell(最後要空間釋放)   

if (!cell) {     

cell = [[[AddressBookCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier]autorelease];   

}

  return cell;
}
6.設定tableView的每一行的編輯狀態(YES,可編輯)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
  return YES
}

7.edit按鈕的點擊事件(當點擊edit按鈕時觸發)

- (void)setEditing:(BOOL)editing animated:(BOOL)animated


8.當提交編輯操作時觸發

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

9.設定tableView每一行是否允許移動(YES,可移動)

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{
  return YES
}

10.提交移動操作之後觸發

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath


UITableViewDelegate協議中常用方法

1.設定行高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{
  return 55;
}

2.選中cell時觸發

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

3.設定tableViewCell的編輯樣式(插入/刪除)

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

4.設定當點擊編輯按鈕時上面顯示的文字,如顯示刪除

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPathNS_AVAILABLE_IOS(3_0) {   return@"刪除"; }


5.設定cell移動的位置

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

iOS ,UITableViewDataSource 和 UITableViewDelegate協議中常用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.