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];
}
}