iOS開發:UITableView基本使用方法

來源:互聯網
上載者:User

iOS開發:UITableView基本使用方法

   UITableView基本使用方法

  1.首先,Controller需要實現兩個delegate ,分別是UITableViewDelegate 和UITableViewDataSource

  2.然後 UITableView對象的 delegate要設定為 self。

  3.然後就可以實現這些delegate的一些方法拉。

  (1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

  這個方法返回 tableview 有多少個section

  //返回有多少個Sections

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

  {

  return 1;

  }

  (2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;

  這個方法返回 對應的section有多少個元素,也就是多少行。

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

  {

  return 10;

  }

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

  這個方法返回指定的 row 的高度。

  - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

  這個方法返回指定的 section的header view 的高度。

  - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

  這個方法返回指定的 section的footer view 的高度。

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

  {

  static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";

  UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];

  if (cell == nil)

  {

  // Create a cell to display an ingredient.

  cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle

  reuseIdentifier:showUserInfoCellIdentifier]

  autorelease];

  }

  // Configure the cell.

  cell.textLabel.text=@"簽名";

  cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str() encoding:NSUTF8StringEncoding];

  }

  (4)- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

  返回指定的 section 的header的高度

  - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

  {

  if (section ==0)

  return 80.0f;

  else

  return 30.0f;

  }

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

  返回指定的section 的 header 的 title,如果這個section header 有返回view,那麼title就不起作用了。

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

  {

  if (tableView == tableView_)

  {

  if (section == 0)

  {

  return @"title 1";

  }

  else if (section == 1)

  {

  return @"title 2";

  }

  else

  {

  return nil;

  }

  }

  else

  {

  return nil;

  }

  }

  (6) - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

  返回指定的 section header 的view,如果沒有,這個函數可以不返回view

  - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

  {

  if (section == 0)

  {

  UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView"

  owner: self

  options: nil] lastObject];

  else

  {

  return nil;

  }

  }

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

  當使用者選中某個行的cell的時候,回調用這個。但是首先,必須設定tableview的一個屬性為可以select 才行。

  TableView.allowsSelection=YES;

  cell.selectionStyle=UITableViewCellSelectionStyleBlue;

  如果不希望響應select,那麼就可以用下面的代碼設定屬性:

  TableView.allowsSelection=NO;

  下面是響應select 點擊函數,根據哪個section,哪個row 自己做出響應就好啦。

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

  {

  if (indexPath.section == 1)

  {

  return;

  }

  else if(indexPath.section==0)

  {

  switch (indexPath.row)

  {

  //聊天

  case 0:

  {

  [self onTalkToFriendBtn];

  }

  break;

  default:

  break;

  }

  }

  else

  {

  return ;

  }

  }

  如何讓cell 能夠響應 select,但是選中後的顏色又不發生改變呢,那麼就設定

  cell.selectionStyle = UITableViewCellSelectionStyleNone;

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

  {

  //cell被選中後的顏色不變

  cell.selectionStyle = UITableViewCellSelectionStyleNone;

  }

  (8)如何設定tableview 每行之間的分割線

  self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;

  如果不需要分割線,那麼就設定屬性為 UITableViewCellSeparatorStyleNone 即可。

  (9)如何設定 tableview cell的背景顏色

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

  {

  //設定背景顏色

  cell.contentView.backgroundColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1];

  }

  (10) - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

  這個函數響應,使用者點擊cell 右邊的 箭頭(如果有的話)

  (11)如何設定tableview 可以被編輯

  首先要進入編輯模式:

  [TableView setEditing:YES animated:YES];

  如果要退出編輯模式,肯定就是設定為NO

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

  返回當前cell 要執行的是哪種編輯,下面的代碼是返回刪除模式

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

  {

  return UITableViewCellEditingStyleDelete;

  }

  -(void) tableView:(UITableView *)aTableView

  commitEditingStyle:(UITableViewCellEditingStyle) editingStyle

  forRowAtIndexPath:(NSIndexPath *)indexPath

  通知告訴使用者編輯了 哪個cell,對應上面的代碼,我們在這個函數裡面執行刪除cell的操作。

  -(void) tableView:(UITableView *)aTableView

  commitEditingStyle:(UITableViewCellEditingStyle) editingStyle

  forRowAtIndexPath:(NSIndexPath *)indexPath

  {

  [chatArray removeObjectAtIndex:indexPath.row];

  [chatTableView reloadData];

  }

  (12)如何獲得 某一行的CELL對象

  - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

相關文章

聯繫我們

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