tableView

tableView是我們做項目最常用的一個組件了,它的功能也非常的多。近期整理了自己曾經(jīng)的筆記,現(xiàn)歸納總結分享給大家,有不足望指正修改。

1、通過tag在cellForRowAtIndexPath中獲取當前cell內的控件
let label = cell.viewWithTag(1000) as! UILabel

2、ios7以后,防止tableView留白問題,在viewDidLoad里面設置
self.automaticallyAdjustsScrollViewInsets = NO

3、去除選中背景
self.tableView.allowsSelection = NO

4、去除分割線
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone
去掉多余的分割線,設置下面的就行了
UITableView的tableFooterView = UIView()

5、選中cell的狀態(tài)抬起后消失
tableView.deselectRowAtIndexPath(indexPath, animated: true)
設置cell不可選中
cell.selectionStyle = UITableViewCellSelectionStyleNone
擺脫選中單元格時出現(xiàn)藍色顯示
self.tableView.allowsSelection = NO

6、禁止tableView滾動
tableview.userInteractionEnabled = false

tableView.scrollEnabled = false

7、讓tableView上層的View隱藏
[firstViewsetHidden:YES];

8、設置cell的選擇標記

if cell.accessoryType == .None {
   cell.accessoryType = .Checkmark
}else{
  cell.accessoryType = .None
}

9、去掉cell右邊的箭頭
cell.accessoryType = UITableViewCellAccessoryNone
設置右側箭頭
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator

10、返回當前顯示的數(shù)組
tableView.visibleCells

11、根據(jù)cell獲取對應的row
let indexPaths:NSIndexPath = tableView.indexPathForCell(cell)!
獲取當前的cell
let btnPos:CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
let indexPath:NSIndexPath = self.tableView.indexPathForRowAtPoint(btnPos)!

12、滾動條的偏移量
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 49, 0)

13、設置HeadView隨著cell一起滾動
將table設置為UITableViewStyleGrouped類型。tableView中的headView就可以table滾動。

14、自適應高度
第一步
super.viewDidLoad()
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
第二步
設置label的約束,記得不要設置高度,把lines設為0
第三步
在Content Hugging Priority里修改Vertical為250

15、動態(tài)會變化的cell
cell.updateConstraintsIfNeeded()

16、半透明的tableview
self.modalPresentationStyle = .Custom

17、默認選中cell
NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:ip animated: YES scrollPosition: UITableViewScrollPositionBottom]

標記、移動、刪除、插入

1、標記

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    UITableViewCell *cellView = [tableView cellForRowAtIndexPath:indexPath];  
    if (cellView.accessoryType == UITableViewCellAccessoryNone) {  
        cellView.accessoryType=UITableViewCellAccessoryCheckmark;  
    } else {  
    cellView.accessoryType = UITableViewCellAccessoryNone;  
    [tableView deselectRowAtIndexPath:indexPath animated:YES];  
    }  
} 
UITableViewCellAccessoryCheckmark                         
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryDisclosureIndicator  
UITableViewCellAccessoryNone

2、移動
實現(xiàn)移動單元格就需要把單元格的編輯屬性設置為YES,[tableView setEditing:YES animated:YES];

//返回YES,表示支持單元格的移動  
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    return YES;  
} 
 //單元格返回的編輯風格,包括刪除 添加 和 默認  和不可編輯三種風格  
 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    return UITableViewCellEditingStyleInsert;  
}
UITableViewCellEditingStyleDelete               
UITableViewCellEditingStyleInsert
UITableViewCellEditingStyleNone

// 實現(xiàn)移動的方法
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath  
{  
//    需要的移動行  
    NSInteger fromRow = [sourceIndexPath row];  
//    獲取移動某處的位置  
    NSInteger toRow = [destinationIndexPath row];  
//    從數(shù)組中讀取需要移動行的數(shù)據(jù)  
    id object = [self.listData objectAtIndex:fromRow];  
//    在數(shù)組中移動需要移動的行的數(shù)據(jù)  
    [self.listData removeObjectAtIndex:fromRow];  
//    把需要移動的單元格數(shù)據(jù)在數(shù)組中,移動到想要移動的數(shù)據(jù)前面  
    [self.listData insertObject:object atIndex:toRow];    
}

3、刪除
首先是判斷(UITableViewCellEditingStyle)editingStyle

  //單元格返回的編輯風格,包括刪除 添加 和 默認  和不可編輯三種風格  
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  {  
   return UITableViewCellEditingStyleDelete;  
 }  

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  
{  
   if (editingStyle==UITableViewCellEditingStyleDelete) {  
  //        獲取選中刪除行索引值  
        NSInteger row = [indexPath row];  
 //        通過獲取的索引值刪除數(shù)組中的值  
        [self.listData removeObjectAtIndex:row];  
 //        刪除單元格的某一行時,在用動畫效果實現(xiàn)刪除過程  
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];  
   }  
}

4、插入(添加)
實現(xiàn)方法和刪除方法相同,首先還是返回單元格編輯風格

//單元格返回的編輯風格,包括刪除 添加 和 默認  和不可編輯三種風格  
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
     return UITableViewCellEditingStyleInsert;  
}
 // 為了顯示效果明顯,在.h文件中聲明一個變量i
    NSInteger i;  
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    if (editingStyle==UITableViewCellEditingStyleDelete) {  
//        獲取選中刪除行索引值  
        NSInteger row = [indexPath row];  
//        通過獲取的索引值刪除數(shù)組中的值  
        [self.listData removeObjectAtIndex:row];  
//        刪除單元格的某一行時,在用動畫效果實現(xiàn)刪除過程  
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];  
    }  
    if(editingStyle==UITableViewCellEditingStyleInsert)  
    {  
         
        i=i+1;  
        NSInteger row = [indexPath row];  
        NSArray *insertIndexPath = [NSArray arrayWithObjects:indexPath, nil];  
        NSString *mes = [NSString stringWithFormat:@"添加的第%d行",i];  
//        添加單元行的設置的標題  
        [self.listData insertObject:mes atIndex:row];  
        [tableView insertRowsAtIndexPaths:insertIndexPath withRowAnimation:UITableViewRowAnimationRight];  
    }  
}  
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,090評論 3 38
  • 1、UITableViewDataSource數(shù)據(jù)源方法 // 返回第section組中有多少行 -(NSInte...
    小楊的app閱讀 1,528評論 0 1
  • 讓我們共同學習一下tableView聯(lián)動,我這也是從簡書上看來的一篇文章,來親自實現(xiàn)一下。學習文章地址:http:...
    麥穗0615閱讀 3,868評論 4 55
  • 讓我們共同學習一下tableView聯(lián)動,我這也是從簡書上看來的一篇文章,來親自實現(xiàn)一下。學習文章地址:http:...
    搬磚行家閱讀 315評論 0 1
  • 藍翔技師廖勛欽?高級工學校辦學31年,累計為社會培養(yǎng)各類各層次技能人才40余萬,其中成功創(chuàng)業(yè)的中小微企業(yè)以數(shù)萬計。...
    此寡百都鼐chuil閱讀 235評論 0 0