以下總結的方法和屬性,是我自己在平時的開發中經常使用到的,實用性很強,紅字的是比較重要卻又不太好記住的方法,大家可以拿去看一下!如果哪裏出現錯誤,歡迎指出來,大家一起討論學習!
首先是tableView兩個代理的常用方法:
1.UITableViewDelegate的方法
1>點擊某個cell執行什麼操作的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}
2.UITableViewDataSource的方法
1>有幾個組
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{}
2>每個組有幾個cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{}
3>每個組的每個cell裏面的內容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}
//設置section的頭部標題(尾部標題Footer)
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {}
//設置頭部視圖(尾部視圖footerView)
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {}
3.UItableView的常用方法
//重新加載tableView,刷新全局
[self.tableView reloadData];
//重新加載tableView中部分的cell
[self.tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];
如何獲取當前模型呢 就要獲取當前cell的索引 方法如下
NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow];
//專門獲取組的索引的方法,需要給headerView設置一個tag,headerView.tag=section
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:headerView.tag];
//刷新指定組的方法
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationLeft];
//獲得最後一行的索引
NSIndexPath *lastPath = [NSIndexPath indexPathForRow:self.messageFrame.count- 1 inSection:0];
//讓tableView滾動到哪一行的哪個部位
[self.tableView scrollToRowAtIndexPath:lastPathatScrollPosition:UITableViewScrollPositionBottom animated:YES];
4.UITableViewCell的常用屬性
// 設置cell上面三個子控件
cell.imageView.image = [UIImage imageNamed:hero.icon];
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
cell的類型 :UITableViewCellStyle
cell的分割線類型:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
cell被選中的類型:UITableViewCellSelectionStyle
cell的尾部按鈕類型(輔助樣式):UITableViewCellAccessoryType
cell不允許點擊 : self.tableVIew.allowsSelection = NO;
// 自定義輔助指示器
cell.accessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
//要想cell透明來顯示tableView的背景圖片 就要把顏色設成ClearColor
cell.backgroundColor = [UIColor ClearColor];
// 設置cell默認狀態的背景視圖
cell.backgroundView = imageView;
// 設置cell選中時的背景視圖
cell.selectedBackgroundView = imageView;
//創建(返回)索引欄
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView*)tableView {
NSMutableArray * arrM = [NSMutableArray array];
for (RCGroups *group in self.groups) {
[arrM addObject:group.title];
}
return arrM;
}