【Coding】【3】iOS UI小操作集(一)

本文最后更新于:2021年12月22日 上午

【Coding】系列目录


目录

1. UITableViewCell取消点击高亮

2. UITableView取消选中状态

3. UITableView去除分割线

4. UICollectionViewCell选中高亮

  4.1 控制代理方法

  4.2 控制UICollectionViewCell

5. iOS长按菜单UIMenuController


内容

1. UITableViewCell取消点击高亮

可以在tableView: cellForRowAtIndexPath:数据源方法中调用下面代码

1
cell.selectionStyle = UITableViewCellSelectionStyleNone;   

自定义cell可以在cell中调用上述代码

2. UITableView取消选中状态

1
2
3
4
5
6
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//其他操作

// 取消选中状态
[tableView deselectRowAtIndexPath:indexPath animated:NO]; 
}

3. UITableView去除分割线

1
tableView.separatorStyle = UITableViewCellSeparatorStyleNone
1
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, cell.bounds.size.width);

若需要隐藏某个cell的分割线,而不是所有的,可以判断indexPath调用第二种方式

4. UICollectionViewCell选中高亮

4.1 控制代理方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{

UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = YouColor;

}
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{

UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{

return YES;
}

4.2 控制UICollectionViewCell

1
2
self.selectedBackgroundView = [[UIView alloc] init];
self.selectedBackgroundView.backgroundColor = YouColor;

5. iOS长按菜单UIMenuController

  • 初始化UIMenuItem

    1
    2
    UIMenuItem *menuCopy = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(menuCopyAction:)];
    UIMenuItem *menuDelete = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(menuDeleteAction:)];
  • 初始化UIMenuController

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    UIMenuController *menu = [UIMenuController sharedMenuController];
    //菜单箭头方向
    menu.arrowDirection = UIMenuControllerArrowDefault;
    //添加Item内容
    [menu setMenuItems:[NSArray arrayWithObjects:menuCopy, menuDelete, nil]];
    //设置位置与添加视图(可以给cell的frame)
    //[menu setTargetRect:CGRectMake(100, 100, 100, 20) inView:self.view];
    [menu setTargetRect:menuButton.frame inView:self.view];
    //显示菜单MenuVisible:并且带动画animated:
    [menu setMenuVisible:YES animated:YES];

  • 实现响应方法

    1
    2
    3
    4
    5
    6
    7
    - (void)menuCopyAction:(id)sender {

    }

    - (void)menuDeleteAction:(id)sender {

    }
  • 重写方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    #pragma mark - ---- Override
    - (BOOL)canBecomeFirstResponder {
    return YES;
    }

    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(menuCopyAction:)) {
    return YES;
    } else if (action == @selector(menuDeleteAction:)) {
    return YES;
    }
    return NO;

    }

Tips
当页面消失或者列表滚动,可以选择隐藏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#pragma mark - ---- Override
- (void)viewWillDisappear:(BOOL)animated {
[self hideMenuController];
[super viewWillDisappear:animated];
}

#pragma mark - ---- UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[self hideMenuController];
}

// 隐藏
- (void)hideMenuController {
UIMenuController * menu = [UIMenuController sharedMenuController];
[menu setMenuVisible:NO animated:YES];
}

联系方式

邮箱: adrenine@163.com