日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

iOS学习之UItableView

發(fā)布時(shí)間:2023/12/10 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS学习之UItableView 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一些相關(guān)的總結(jié),有點(diǎn)亂.

  • ?UITableViewiOS中提供的用來以列的形式展示數(shù)據(jù)的視圖,叫做表現(xiàn)圖,但是只有一列,而且只能在垂直方向滾動(dòng).繼承自UIScrollView.
  • ?UITableView由多個(gè)分區(qū)組成(相當(dāng)于班級(jí)的分組),每個(gè)分區(qū)由多行組成(相當(dāng)于每個(gè)分組下的人).
  • ?UITableView有兩種樣式,PlainGroup樣式,一旦設(shè)置之后,后期不能更改.?

?


?

繼承自UITableViewController 繼承自UIViewController的區(qū)別. (UITableViewControllerUIViewController的子類)

  • .前者根視圖是tableView, 而后者根視圖是UIView. 前者不需要指定dataSource,delegate.服從協(xié)議. 而后者需要.
  • 前者不需要重寫setEditing:animated:方法控制tableView進(jìn)入編輯狀態(tài),而后者需要自己實(shí)現(xiàn).
  • 前者對(duì)于UITableViewDataSource協(xié)議中的常用方法已經(jīng)自動(dòng)生成,而后者需要自己添加對(duì)應(yīng)的方法.

何時(shí)需要繼承自UITableViewController?

? ? 當(dāng)前頁(yè)面信息的展示主要是以列的形式來展示的場(chǎng)景下, 都可以直接繼承自UITableViewController.

? ? 在繼承自UITableViewController的視圖控制器中訪問tableView.

? ? 1.self.view? 根視圖就是tableView.

? ? 2.self.tableView 有對(duì)應(yīng)的tableView屬性.


UITableView協(xié)議中的一些方法

UITableViewDataSource協(xié)議

  1.配置TableView一共有幾個(gè)分組

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

  2.配置tableView每個(gè)分區(qū)對(duì)應(yīng)的行數(shù)

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

    3.配置用來顯示每一行數(shù)據(jù)的cell.(UITableViewCell)

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {//1.創(chuàng)建重用標(biāo)識(shí)符.static NSString *identifier = @"heihei";//2.根據(jù)重用標(biāo)識(shí)符去重用隊(duì)列中取可重用的cell.UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];//3.判斷是否成功取到可重用的cell.cell是否為空.if (!cell) {//4.cell為空,說明沒有成功取到cell.則創(chuàng)建一個(gè)cell.cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease];cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //輔助視圖樣式,小箭頭 }NSDictionary *dic = self.addressDic[self.sortedKeys[indexPath.section]][indexPath.row];cell.textLabel.text = dic[@"name"];cell.detailTextLabel.text = dic[@"phone"];cell.imageView.image = [[UIImage imageNamed:dic[@"imageName"]] scaleToSize:CGSizeMake(40, 40)];return cell; }

?

    4.配置每個(gè)分區(qū)的頁(yè)眉 

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

    5.配置tableView右側(cè)的分區(qū)索引

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

    //編輯相關(guān)的協(xié)議方法

    6.設(shè)置tableView的哪些行可以允許編輯

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

    7.提交編輯操作時(shí)觸發(fā)(默認(rèn)的時(shí)刪除操作)

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

//提交編輯操作, 對(duì)刪除操作作出處理. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {//總共分兩步:1.修改數(shù)據(jù)源 2.修改界面//1.獲取刪除行對(duì)應(yīng)的分區(qū)key.(是B分組,還是C分組)NSString *key = self.sortedKeys[indexPath.section];//2.根據(jù)key獲取對(duì)應(yīng)的可變數(shù)組.NSMutableArray *group = self.addressDic[key];if (editingStyle == UITableViewCellEditingStyleInsert) {//處理插入操作//1.修改數(shù)據(jù)源NSDictionary *dic = @{@"name":@"Frank", @"age":@"18", @"gender":@"man", @"phone":@"110", @"imageName":@""};[group insertObject:dic atIndex:indexPath.row];//2.修改界面 [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];} else {//處理刪除操作//需要判斷是否要?jiǎng)h除一個(gè)分區(qū).if (group.count == 1) {//刪除分區(qū)//1.修改數(shù)據(jù)源//從字典中根據(jù)key移除對(duì)應(yīng)的元素. [self.addressDic removeObjectForKey:key];//從排好序的key值數(shù)組中移除對(duì)應(yīng)的key. [self.sortedKeys removeObject:key];//2.修改界面NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];[tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationRight];} else {[group removeObjectAtIndex:indexPath.row]; //刪除行對(duì)應(yīng)的字典.//刪除界面上的一行. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];}} }

?

    //移動(dòng)相關(guān)的協(xié)議方法

    8.設(shè)置tableView哪些行可以允許移動(dòng)

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

    9.提交移動(dòng)操作觸發(fā).

    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;

//提交移動(dòng)操作. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {//因?yàn)橐苿?dòng)操作界面已經(jīng)發(fā)生變化,我們只需要修改數(shù)據(jù)源即可.//1.獲取到分區(qū)對(duì)應(yīng)的數(shù)組.NSMutableArray *group = self.addressDic[self.sortedKeys[sourceIndexPath.section]];//分區(qū)對(duì)應(yīng)的數(shù)組//2.將原位置對(duì)應(yīng)的元素取出來保存.NSDictionary *dic = [group[sourceIndexPath.row] retain]; //retain 引用計(jì)數(shù)加1, 否則移除時(shí)就造成引用計(jì)數(shù)為0,空間回收了.//3.將原位置對(duì)應(yīng)的元素刪除掉. [group removeObjectAtIndex:sourceIndexPath.row];//4.將保存的元素插入到目的位置. [group insertObject:dic atIndex:destinationIndexPath.row];//5.釋放所有權(quán) [dic release]; }

?

?

  UITableViewDelegate協(xié)議

    1.當(dāng)tableView的行被選中時(shí)觸發(fā)

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

    2.當(dāng)tableView的行被取消選中時(shí)觸發(fā)

?

    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;

    3.配置tableView某一行的高度

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

    //編輯相關(guān)

    4.設(shè)置tableView的編輯樣式

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

    5.設(shè)置刪除時(shí)確認(rèn)按鈕的標(biāo)題.

    - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath;

    //移動(dòng)相關(guān)

    6.設(shè)置tableView限制跨區(qū)移動(dòng)

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

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {//sourceIndexPath 移動(dòng)之前的位置//proposedDestinationIndexPath 即將要移動(dòng)到的位置if (sourceIndexPath.section == proposedDestinationIndexPath.section) {return proposedDestinationIndexPath;}return sourceIndexPath;}

?

?


?

UITableView編輯步驟:

? ? 1.在導(dǎo)航條上添加Edit按鈕. 重寫setEditing:Animated:方法.

self.navigationItem.rightBarButtonItem = self.editButtonItem;

?

? ? 2.控制tableView的可編輯狀態(tài).

? ? 3.設(shè)置tableView的哪些行可以允許編輯. (dataSource)

? ? 4.設(shè)置編輯樣式. (delegate)

? ? 5.提交編輯操作. (dataSource) (1)修改數(shù)據(jù)源 (2)修改界面


?

轉(zhuǎn)載于:https://www.cnblogs.com/ErosLii/p/4498881.html

總結(jié)

以上是生活随笔為你收集整理的iOS学习之UItableView的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。