/**UItableView 的几个代理方法*/
1. tableView里面有多少个section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
2. section标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *string = [NSString stringWithFormat:@"水浒%ld", section];
return string; //水浒1, 水浒2
}
3.索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [NSArray arrayWithObjects:@"aa", @"bb", @"cc", nil];
}
4. tableView的 delegate已经签订好scrollView的协议,只要设置好代理人,就可以使用 scrollView的协议方法
5. 去除tableView的边线
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
/* UITableViewCellSeparatorStyleNone, // 枚举类型
UITableViewCellSeparatorStyleSingleLine,
UITableViewCellSeparatorStyleSingleLineEtched // This separator style is only supported for grouped style table views currently */
6.使用注册的方式创建的cell,必须使用自定义的cell, 否则在里面会重复大量的创建视图
7. 不让navigationController的 试图里边 scrollVIew它的子类的试图偏移量自动往下偏移64个单位
self.automaticallyAdjustsScrollViewInsets = NO
8. 导航视图控制器高度是44,上面的状态栏高度是20, 加在一起默认是64
/** tableView 菜单部分*/
9. 设置是否允许给 tableView上的cell添加菜单
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
10. 这个方法是设置是否允许给tableView上的cell添加事件
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
11. 当我们点击菜单上的按钮之后会触发的方法
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
12. viewDidLoad这个方法只执行一次,可以设置行高,但是不灵活
[self.tableView setRowHeight:100]
13. 这个方法是tableView 的 delegate 所提供的协议方法, 主要是用来设置每一行的高度的
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UIImage *image = [UIImage imageNamed:self.picArray[indexPath.row]];
CGFloat rowHeight = image.size.height / image.size.width * kSize.width;// 得到图片高度
// 计算label 高度
//根据对应的文字求出cell上label显示的高度
NSDictionary *fontDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil];
//根据文字的大小, 计算出文本的尺寸
//还需要执行一个尺寸(375, 0)
//第三个参数: 计算高度需要一句字体的哪个特征来确定
CGRect rect = [self.ziArray[indexPath.row] boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil]; //得到文字高度
return rowHeight + rect.size.height;
}
/*tableView的可编辑模式*/
14. 可直接打开tableview的可编辑模式
[self.tableView setEditing:NO animated:YES];
15.重写系统的编辑按钮点击触发的方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
}
16.设置哪些行可以编辑: 默认是 YES(可以编辑)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
17. 编辑模式下 拖拽移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSString *string = [self.nameList[sourceIndexPath.row] retain];// 1. 先获取到起始位置的数据
[self.nameListremoveObjectAtIndex:sourceIndexPath.row]; // 2.把起始位置的对象从数据源中移除
[self.nameListinsertObject:string atIndex:destinationIndexPath.row];// 3. 把数据插入到数组的目的位置上去
[string release];
}
18. 设置删除按钮的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"来点我";
}
19.这个方法是 iOS8.0之后出现的方法,可以在编辑状态的时候有多个按钮
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"触发了删除按钮");// 点击 "删除" 的时候会触发
}];
[deleteAction setBackgroundColor:[UIColor yellowColor]];
UITableViewRowAction *topAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"聊天顶置" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"触发了聊天顶置按钮");// 点击 "聊天頂置" 的时候会触发
}];
[topAction setBackgroundColor:[UIColor magentaColor]];
return @[deleteAction, topAction];
}
20. 删除数据
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(@"%s", __FUNCTION__);
[self.nameListremoveObjectAtIndex:indexPath.row]; //删除 数据源
[self.tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationTop]; //第一个参数: 指定删除哪一个分区的哪个行, 把他作为一个元素放在数组中第二个参数: 删除动画
}
}
21.返回tableView 编辑类型
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
/** cell 上添加快捷菜单 */
22. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:selfaction:@selector(click:)]; // 创建手势
[cell addGestureRecognizer:longPress]; // 添加手势
longPress.minimumPressDuration = 2.0; // 设置最小长按时间
[longPress release]; // 释放
}
cell.textLabel.text = self.nameList[indexPath.row];
return cell;
}
- (void)click:(UILongPressGestureRecognizer *)longPress { // cell 的长按手势 绑定的方法(长按便会触发)
#warning 长按方法 如何只执行一次 答案: 判断state;
if (longPress.state != UIGestureRecognizerStateBegan) return;
NSLog(@"%s", __FUNCTION__);
UITableViewCell *cell = (UITableViewCell *)longPress.view;//通过手势, 找到手势所添加的cell
UIMenuController *menu = [UIMenuController sharedMenuController];//创建一个快捷菜单
[menu setTargetRect:cell.frame inView:cell.superview];//给这个快捷菜单进行定位
UIMenuItem *flag = [[UIMenuItemalloc] initWithTitle:@"测试"action:@selector(flag)];//如果想使用自定义的功能
[menu setMenuItems:@[flag]];//把 这个按钮放到快捷菜单上
[menu setMenuVisible:YES animated:YES];// 让菜单显示出来
//按钮的方法必须要实现,无论系统还是自定义,如果不实现对应的方法,不会添加到快捷菜单上
}
23.快捷菜单捆绑了一个方法, 这个方法必须实现, 如果不实现, 快捷菜单没有办法显示
- (BOOL)canBecomeFirstResponder {
return YES;
}
24. 如果注释了 delete, copy, select就不能在快捷菜单上显示(此时快捷菜单只显示自定义的flag)
//- (void)delete:(id)sender {
// NSLog(@"%s", __FUNCTION__);
//}
//- (void)copy:(id)sender {
// NSLog(@"%s", __FUNCTION__);
//}
//- (void)select:(id)sender {
// NSLog(@"%s", __FUNCTION__);
//}
25. 得到 tableView所选中的 indexPath
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
26. 松手之后,以动画的形式取消选中状态
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
27. tableViewCell 的 选中样式
self.selectionStyle = UITableViewCellSelectionStyleDefault;