tableview&&iOS学习笔记iOS Developer

iOS自定义UICollectionView和UITableVi

2017-06-16  本文已影响1077人  小码僧

iOS中,UICollectionView和UITableView已经有系统默认选中颜色设置,但是只有无色,蓝色,灰色,三种颜色设置,如果想要其他的颜色效果,我们可以自由自定义设置。

前言

image.png image.png

1.单元格默认选中效果

//无色
cell.selectionStyle = 
UITableViewCellSelectionStyleNone
;
//蓝色
cell.selectionStyle = 
UITableViewCellSelectionStyleBlue
;
//灰色
cell.selectionStyle = 
UITableViewCellSelectionStyleGray
;
cell.selectionStyle = UITableViewCellStyleDefault;
cell.selectionStyle = UITableViewCellSelectionStyleDefault;

示例

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([MyTableViewCell class]) owner:self options:nil] lastObject];
    }
    cell.cellMdl = [self.tableItemArr objectAtIndex:indexPath.row];
    
    //设置选中背景色
    cell.selectionStyle = UITableViewCellStyleDefault;
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
}

2.单元格自定义选中效果方案(一)

假设你已经正确实现其他代理方法,需要在table或collection的返回cell的代理方法中作如下设置:

cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
cell.selectedBackgroundView.backgroundColor = [UIColor groupTableViewBackgroundColor];

示例:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
//    return [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCellId" forIndexPath:indexPath];
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionCellId forIndexPath:indexPath];
    
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
////    错误的做法:当次级VC返回时才会调用
//    if (cell.isHighlighted) {
//        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
//    }else{
//        cell.backgroundColor = [UIColor whiteColor];
//    }
    
    return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    AssistantTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([AssistantTableViewCell class]) owner:self options:nil] lastObject];
    }
    cell.cellMdl = [self.questionItemArr objectAtIndex:indexPath.row];
    
    //设置选中背景色
//    cell.selectionStyle = UITableViewCellStyleDefault;
//    cell.selectionStyle = UITableViewCellSelectionStyleDefault;

    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    return cell;
}

3.单元格自定义选中效果方案(二)

示例:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    [super setHighlighted:highlighted];
    if (highlighted) {
        //选中时
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }else{
        //非选中
        self.backgroundColor = [UIColor whiteColor];
    }
}

- (void)setHighlighted:(BOOL)highlighted{
    [super setHighlighted:highlighted];
    if (highlighted) {
        //选中时
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }else{
        //非选中
        self.backgroundColor = [UIColor whiteColor];
    }
}

4.小结

如你所见,不难发现,两个cell设置套路是一样的(捂脸)。

注意的是,方案一和方案二不要重复设置。另外,二者择一的话,推荐方案一。

上一篇 下一篇

猜你喜欢

热点阅读