以下总结的方法和属性,是我自己在平时的开发中经常使用到的,实用性很强,红字的是比较重要却又不太好记住的方法,大家可以拿去看一下!如果哪里出现错误,欢迎指出来,大家一起讨论学习!
首先是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;
}