UI表格的单选制作

过程步骤

1.AppDelegate.m文件包含ViewController.h文件。

#import “ViewController.h”

  • 创建ViewController对象(vc)

ViewController *vc=[[ViewController alloc]init];

-创建UINavigationController对象(nVC)

-把nVC设置为自己的根试图 -设置self.window.rootViewController为nVC
UINavigationController *nVC = [[UINavigationController alloc]initWithRootViewController:vc];
代码为:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //实现首页的设置
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]];//创建窗口
    ViewController *vc=[[ViewController alloc]init];//创建控制器
    vc.title = @"QQ列表";//导航栏标题
    UINavigationController *nVC = [[UINavigationController alloc]initWithRootViewController:vc];//创建导航栏控制器,并设置根试图控制器
    self.window.rootViewController = nVC;//设置window跟试图为导航栏控制器
    [self.window makeKeyAndVisible];//大多数应用程序调用它来显示主窗口,并使其成为关键。否则使用视图中隐藏属性
    return YES;
}

2.在ViewController.m中创建全局可变数组,并添加表格需要的数据字典对象

#import "ViewController.h"

@interface ViewController ()
{
    NSMutableArray *arrAllDatas;
}
@end

@implementation ViewController

- (void)viewDidLoad
 {
    [super viewDidLoad];
    //初始化
    arrAllDatas = [NSMutableArray new];
    //给数组里面添加5个可变字典对象
    for (int i=0; i<5; i++)
    {
        UIImage *img =[UIImage imageNamed:@"qq"];
        NSString *nickname=@"披星戴月";
        NSString *describe=@"不要把自己的希望寄托在别人身上";
        NSString *select=@"No";
        
        //初始化可变字典
        NSMutableDictionary *dic =[NSMutableDictionary new];
        //根据关键字添加值
        [dic setObject:img forKeyedSubscript:@"imgHead"];
        [dic setObject:nickname forKeyedSubscript:@"nickname"];
        [dic setObject:describe forKeyedSubscript:@"describe"];
        [dic setObject:select forKeyedSubscript:@"select"];
        
        //添加字典对象到数组中
        [arrAllDatas addObject:dic];
    }
}

-创建类QQListView,继承UIView,并在QQListView.h中声明初始方法

新建一个UIVIew下的类

QQListView.h中补充协议

@interface QQListView : UIView<UITableViewDataSource,UITableViewDelegate>//加上协议代理

-代理方法和初始化器


#import <UIKit/UIKit.h>

@interface QQListView : UIView<UITableViewDataSource,UITableViewDelegate>//加上协议代理
{
    UITableView *_tabliView;//表格
    NSMutableArray *_arrData;//用来存放单元格数据
}

/**
 *  声明重写初始化方法
 */
-(instancetype)initWithFrame:(CGRect)frame
                  andDataArr:(NSMutableArray*)arrData;
@end

-在QQListView.m文件中实现重写的初始化方法

#import "QQListView.h"
#import "ViewController.h"
@implementation QQListView

-(instancetype)initWithFrame:(CGRect)frame
andDataArr:(NSMutableArray *)arrData
{
    if (self=[super initWithFrame:frame])
    {
        _arrData=[NSMutableArray new];//初始化数组
        _arrData=arrData;//初始化数组,把传进来的表格数据存入_arrData中
        _tabliView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];//挨个初始化
        
        //设置代理和数据来源
        _tabliView.delegate=self;
        _tabliView.dataSource=self;
        
        _tabliView.separatorStyle=UITableViewCellSeparatorStyleNone;//去掉表格的线
        
        [self addSubview:_tabliView];//将表格添加到view中
    }
    return self;
}
@end

3.创建类ListTableViewCell,继承UITablieViewCell

创建新的类

-在ListTableViewCell.h文件中声明单元格空间变量

-在ListTableViewCell.m文件中是实现重写的初始化方法

4.在QQListView.m中实现表格代理方法,包含ListTableViewCell.h文件

-用ListTableViewCell类创建cell对象
在ListTableViewCell.h文件中写

#import <UIKit/UIKit.h>

@interface ListTableViewCell : UITableViewCell
{
    UIImageView *_imgHead;//头像
    UILabel *_lblNickname;//昵称
    UILabel *_lblDescribe;//描述
    UIImageView *_imgChoose;//打钩的图片
    UIImageView *_imgLine;//表格线条
}
@end

ListTableViewCell.m文件中写

#import "ListTableViewCell.h"
#define VIEW_SIZE  [UIScreen mainScreen].bounds.size//屏幕的大小
@implementation ListTableViewCell

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
/**
 *  设置cell内容的方法
 *
 *  @param dic 内容的字典
 */
-(void)setCell:(NSDictionary *)dic
{
    _imgHead.image=dic[@"imghead"];
    _lblNickname.text=dic[@"nickname"];
    _lblDescribe.text=dic[@"describe"];
    _imgChoose.image=[UIImage imageNamed:@"boss_unipay_ic_right"];
    //如果选择是YES就显示打钩图片,否则就隐藏
    if ([dic[@"select"] isEqualToString:@"YES"])
         {
             _imgChoose.hidden=NO;
         }
    else
         {
             _imgChoose.hidden=YES;
         }
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        //初始化控件
        _lblNickname =[UILabel new];
        _lblNickname.font=[UIFont systemFontOfSize:14];
        
        _imgHead=[UIImageView new];
        _imgChoose=[UIImageView new];
        _lblDescribe=[UILabel new];
        _lblDescribe.font=[UIFont systemFontOfSize:12];
        _lblDescribe.textColor=[UIColor grayColor];
        
        _imgLine=[UIImageView new];
        _imgLine.image=[UIImage imageNamed:@"line"];
        
        _imgHead.frame = CGRectMake(10, 5, 50, 50);//头像的大小
        _imgHead.layer.cornerRadius =_imgHead.frame.size.height/2.0;//头像是圆形的
        _imgHead.layer.masksToBounds=YES;//层之间的映射的坐标和时间空间
        
        _lblNickname.frame = CGRectMake(70, 8, VIEW_SIZE.width-110, 20);
        _lblDescribe.frame = CGRectMake(70, 30, VIEW_SIZE.width-110, 20);
        
        _imgChoose.frame=CGRectMake(VIEW_SIZE.width-35, 19, 22, 22);
        
        _imgLine.frame=CGRectMake(0, 59, VIEW_SIZE.width, 1);
        
        //把控件添加到cell的contenView之上
        [self.contentView addSubview:_imgHead];
        [self.contentView addSubview:_lblDescribe];
        [self.contentView addSubview:_imgChoose];
        [self.contentView addSubview:_lblNickname];
        [self.contentView addSubview:_imgLine];
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}
@end

5.在ViewController.m中包含QQListView.h文件

-创建QQListView对象,添加到self.view中

#import "ViewController.h"
#import "QQListView.h"

@interface ViewController ()
{
    NSMutableArray *arrAllDatas;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //================2==========
    
   //初始化数组
    arrAllDatas=[NSMutableArray new];
    //往数组里添加5个可变字典对象
    for (int i=0; i<5; i++)
    {
        UIImage *img=[UIImage imageNamed:@"qq.png"];
        NSString *nickname=@"披星戴月";
        NSString *describe=@"不要把自己的希望寄托在别人身上···";
        NSString *select=@"NO";
        //初始化可变字典对象
        NSMutableDictionary *dic=[NSMutableDictionary new];
        //根据关键字添加值
        [dic setObject:img forKey:@"imgHead"];
        [dic setObject:nickname forKey:@"nickname"];
        [dic setObject:describe forKey:@"describe"];
        [dic setObject:select forKey:@"select"];
        
        //添加字典对象到数组中
        [arrAllDatas addObject:dic];
    }
    
    //创建表格视图
    [self creatListView];
    
}

/**
 *  创建列表视图
 */
-(void)creatListView
{
    QQListView *qqListV=[[QQListView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)  andDataArr:arrAllDatas];
    
    [self.view addSubview:qqListV];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 
    NSMutableDictionary *dic=_arrData[indexPath.row];
    
    if ([dic[@"select"] isEqualToString:@"YES"])
    {
        //如果已经选择,就取消选择,
        [dic setValue:@"NO" forKey:@"select"];
    }
    else
    {
        [dic setValue:@"YES" forKey:@"select"];
    }
    //把团来选中的都改为没选中
    for (int i=0;i<_arrData.count;i++)
    {
        if (i==indexPath.row)//如果是当前选择的行就不做修改
        {
            continue;
        }
        NSMutableDictionary *dic1=_arrData[i];
        [dic1 setValue:@"NO" forKey:@"select"];
        [_arrData  replaceObjectAtIndex:i withObject:dic1];
    }
    //将当前选中的单元格的数据替换修改后的数据
    [_arrData  replaceObjectAtIndex:indexPath.row withObject:dic];
    //刷新表格
    [tableView reloadData];

6.在ListTabbleViewCell.h文件中设置单元格数据方法

-在ListTableViewCell.m文件中实现数据显示方法

#import <UIKit/UIKit.h>

@interface ListTableViewCell : UITableViewCell
{
    UIImageView *_imgHead;//头像
    UILabel *_lblNickname;//昵称
    UILabel *_lblDescribe;//描述
    UIImageView *_imgChoose;//打勾图片
    UIImageView *_imgLine;//表格线条
    
}
//设置单元格数据方法
-(void)setCell:(NSDictionary *)dic;

@end```

###7.在ListTableViewCell.m文件中实现点击单元格触发的代理方法,完成单选功能

import "ListTableViewCell.h"

define VIEW_SIZE [UIScreen mainScreen].bounds.size

@implementation ListTableViewCell
/**

  • 设置cell内容的方法

  • @param dic 内容字典
    */
    -(void)setCell:(NSDictionary *)dic
    {
    _imgHead.image=dic[@"imgHead"];
    _lblNickname.text=dic[@"nickname"];
    _lblDescribe.text=dic[@"describe"];
    _imgChoose.image=[UIImage imageNamed:@"boss_unipay_ic_right@2x.png"];
    //如果选择是YES就显示打勾图片,否则就隐藏
    if ([dic[@"select"] isEqualToString:@"YES"])
    {
    _imgChoose.hidden=NO;
    }
    else
    {
    _imgChoose.hidden=YES;

    }
    }

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
//初始化控件
_lblNickname=[UILabel new];
_lblNickname.font=[UIFont systemFontOfSize:14];

    _imgHead=[UIImageView new];
    _imgChoose=[UIImageView new];
    _lblDescribe=[UILabel new];
    _lblDescribe.font=[UIFont systemFontOfSize:12];
    _lblDescribe.textColor=[UIColor grayColor];
    
    _imgLine=[UIImageView new];
    _imgLine.image=[UIImage imageNamed:@"line@2x"];
   
    
    _imgHead.frame =  CGRectMake(10, 5, 50, 50);//头像大小40*40
    _imgHead.layer.cornerRadius=_imgHead.frame.size.height/2.0;//头像是圆形
    _imgHead.layer.masksToBounds=YES;
    
    _lblNickname.frame = CGRectMake(70, 8, VIEW_SIZE.width-110, 20);
    _lblDescribe.frame = CGRectMake(70, 30, VIEW_SIZE.width-110, 20);
    
    _imgChoose.frame=CGRectMake(VIEW_SIZE.width-35, 19, 22, 22);
    
    _imgLine.frame=CGRectMake(0 , 59, VIEW_SIZE.width , 1);
    //将控件添加到cell的contentView上面
    [self.contentView addSubview:_imgHead];
    [self.contentView addSubview:_lblDescribe];
    [self.contentView addSubview:_imgChoose];
    [self.contentView addSubview:_lblNickname];
    [self.contentView addSubview:_imgLine];
  
}
return self;

}

  • (void)awakeFromNib {
    // Initialization code
    }

  • (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
    }

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,326评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,228评论 1 304
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,979评论 0 252
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,489评论 0 217
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,894评论 3 294
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,900评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,075评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,803评论 0 205
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,565评论 1 249
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,778评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,255评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,582评论 3 261
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,254评论 3 241
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,151评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,952评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,035评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,839评论 2 277

推荐阅读更多精彩内容