iOS学习之UItableView
一些相關的總結,有點亂.
- ?UITableView是iOS中提供的用來以列的形式展示數據的視圖,叫做表現圖,但是只有一列,而且只能在垂直方向滾動.繼承自UIScrollView.
- ?UITableView由多個分區組成(相當于班級的分組),每個分區由多行組成(相當于每個分組下的人).
- ?UITableView有兩種樣式,Plain和Group樣式,一旦設置之后,后期不能更改.?
?
?
繼承自UITableViewController 與 繼承自UIViewController的區別. (UITableViewController是UIViewController的子類)
- .前者根視圖是tableView, 而后者根視圖是UIView. 前者不需要指定dataSource,delegate.服從協議. 而后者需要.
- 前者不需要重寫setEditing:animated:方法控制tableView進入編輯狀態,而后者需要自己實現.
- 前者對于UITableViewDataSource協議中的常用方法已經自動生成,而后者需要自己添加對應的方法.
何時需要繼承自UITableViewController?
? ? 當前頁面信息的展示主要是以列的形式來展示的場景下, 都可以直接繼承自UITableViewController.
? ? 在繼承自UITableViewController的視圖控制器中訪問tableView.
? ? 1.self.view? 根視圖就是tableView.
? ? 2.self.tableView 有對應的tableView屬性.
UITableView協議中的一些方法
UITableViewDataSource協議
1.配置TableView一共有幾個分組
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
2.配置tableView每個分區對應的行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
3.配置用來顯示每一行數據的cell.(UITableViewCell)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {//1.創建重用標識符.static NSString *identifier = @"heihei";//2.根據重用標識符去重用隊列中取可重用的cell.UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];//3.判斷是否成功取到可重用的cell.cell是否為空.if (!cell) {//4.cell為空,說明沒有成功取到cell.則創建一個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.配置每個分區的頁眉
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
5.配置tableView右側的分區索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
//編輯相關的協議方法
6.設置tableView的哪些行可以允許編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
7.提交編輯操作時觸發(默認的時刪除操作)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
//提交編輯操作, 對刪除操作作出處理. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {//總共分兩步:1.修改數據源 2.修改界面//1.獲取刪除行對應的分區key.(是B分組,還是C分組)NSString *key = self.sortedKeys[indexPath.section];//2.根據key獲取對應的可變數組.NSMutableArray *group = self.addressDic[key];if (editingStyle == UITableViewCellEditingStyleInsert) {//處理插入操作//1.修改數據源NSDictionary *dic = @{@"name":@"Frank", @"age":@"18", @"gender":@"man", @"phone":@"110", @"imageName":@""};[group insertObject:dic atIndex:indexPath.row];//2.修改界面 [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];} else {//處理刪除操作//需要判斷是否要刪除一個分區.if (group.count == 1) {//刪除分區//1.修改數據源//從字典中根據key移除對應的元素. [self.addressDic removeObjectForKey:key];//從排好序的key值數組中移除對應的key. [self.sortedKeys removeObject:key];//2.修改界面NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];[tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationRight];} else {[group removeObjectAtIndex:indexPath.row]; //刪除行對應的字典.//刪除界面上的一行. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];}} }?
//移動相關的協議方法
8.設置tableView哪些行可以允許移動
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
9.提交移動操作觸發.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;
//提交移動操作. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {//因為移動操作界面已經發生變化,我們只需要修改數據源即可.//1.獲取到分區對應的數組.NSMutableArray *group = self.addressDic[self.sortedKeys[sourceIndexPath.section]];//分區對應的數組//2.將原位置對應的元素取出來保存.NSDictionary *dic = [group[sourceIndexPath.row] retain]; //retain 引用計數加1, 否則移除時就造成引用計數為0,空間回收了.//3.將原位置對應的元素刪除掉. [group removeObjectAtIndex:sourceIndexPath.row];//4.將保存的元素插入到目的位置. [group insertObject:dic atIndex:destinationIndexPath.row];//5.釋放所有權 [dic release]; }?
?
UITableViewDelegate協議
1.當tableView的行被選中時觸發
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
2.當tableView的行被取消選中時觸發
?
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
3.配置tableView某一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
//編輯相關
4.設置tableView的編輯樣式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
5.設置刪除時確認按鈕的標題.
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath;
//移動相關
6.設置tableView限制跨區移動
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {//sourceIndexPath 移動之前的位置//proposedDestinationIndexPath 即將要移動到的位置if (sourceIndexPath.section == proposedDestinationIndexPath.section) {return proposedDestinationIndexPath;}return sourceIndexPath;}?
?
?
UITableView編輯步驟:
? ? 1.在導航條上添加Edit按鈕. 重寫setEditing:Animated:方法.
self.navigationItem.rightBarButtonItem = self.editButtonItem;?
? ? 2.控制tableView的可編輯狀態.
? ? 3.設置tableView的哪些行可以允許編輯. (dataSource)
? ? 4.設置編輯樣式. (delegate)
? ? 5.提交編輯操作. (dataSource) (1)修改數據源 (2)修改界面
?
轉載于:https://www.cnblogs.com/ErosLii/p/4498881.html
總結
以上是生活随笔為你收集整理的iOS学习之UItableView的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 深信服 云桌面 linux,极域深信服云
- 下一篇: Website for the intr
