zoukankan      html  css  js  c++  java
  • 实现点击cell实现改变cell和cell上控件的背景颜色

    话不多少,贴上代码吧!!!

    //
    //  ViewController.m
    //  CellChangeBgColorDemo
    //
    //  Created by 思 彭 on 17/1/12.
    //  Copyright © 2017年 思 彭. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "TableViewCell.h"
    
    @interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
    
    @property (nonatomic, strong) UITableView *tableView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.view addSubview:self.tableView];
        
        // 注册cell
        [self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"cell"];
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        
        return 10;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        
        return 10;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.titleLabel.text = [NSString stringWithFormat:@"%ld--%ld",indexPath.section,indexPath.row];
        cell.contentView.backgroundColor = [UIColor lightGrayColor];
        cell.selectedBackgroundView = [[UIView alloc]initWithFrame:cell.frame];
        cell.selectedBackgroundView.backgroundColor = [UIColor orangeColor];
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        
    //    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    //    NSIndexPath *selectIndexPath = [self.tableView indexPathForSelectedRow];
    //    NSLog(@"selectIndexPath = %ld---%ld",selectIndexPath.section,selectIndexPath.row);
        TableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.titleLabel.backgroundColor = [UIColor redColor];
        cell.contentView.backgroundColor = [UIColor orangeColor];
    }
    
    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
        
    }
    
    @end
  • 相关阅读:
    tomcat使用入门
    IDEA2020 创建springboot项目提示程序包org.springframework.boot不存在 问题
    jvm内存泄露
    tomcat 上设置可以直接访问的图片路径
    服务器上安装mysql后开启远程连接
    图的遍历,BFS和DFS的Java实现
    并查集
    深度优先搜索实现拓扑排序(leetcode210课程表)
    在Java中怎么实现字符'a'转成字符'b'
    MyBatis底层原理
  • 原文地址:https://www.cnblogs.com/pengsi/p/6277880.html
Copyright © 2011-2022 走看看