单选

作者: ChiCoChiCo | 来源:发表于2016-07-03 21:46 被阅读63次

所在的分组内有且只有一个被选中 效果图如下

849EFA9D-7943-41B3-A982-53235C2CCEA1.png

JSON数据:

 [
  [
   {
   "content" : "所有人",
   "choosed" : true,
   "section_name" : "赞"
   },
   {
   "content" : "朋友",
   "choosed" : false,
   "section_name" : "赞"
   },
   {
   "content" : "关闭",
   "choosed" : false,
   "section_name" : "赞"
   }
   ],
  [
   {
   "content" : "所有人",
   "choosed" : true,
   "section_name" : "评论"
   },
   {
   "content" : "朋友",
   "choosed" : false,
   "section_name" : "评论"
   },
   {
   "content" : "关闭",
   "choosed" : false,
   "section_name" : "评论"
   }
   ],
  [
   {
   "content" : "打开",
   "choosed" : true,
   "section_name" : "关注"
   },
   {
   "content" : "关闭",
   "choosed" : false,
   "section_name" : "关注"
   }
   ],
  [
   {
   "content" : "打开",
   "choosed" : true,
   "section_name" : "PICTURER 消息"
   },
   {
   "content" : "关闭",
   "choosed" : false,
   "section_name" : "PICTURER 消息"
   }
   ]
  ]
//单选模型
#import <Foundation/Foundation.h>

@interface QKDanChooseModel : NSObject

@property (nonatomic,copy)NSString *content;
@property (nonatomic,copy)NSString *section_name;
@property (nonatomic,assign)BOOL choosed;

- (void)upDateChooseState:(NSNumber *)choose;

@end


#import "QKDanChooseModel.h"

@implementation QKDanChooseModel

- (void)upDateChooseState:(NSNumber *)choose
{
    if ([choose integerValue] == 0)
    {
        self.choosed = NO;
        
    }else
    {
        self.choosed = YES;
    }
}

@end
//使用

#import "QKChooseController.h"
#import "QKDanXuanCell.h"
#import "QKReasouce.h"

@interface QKChooseController () <UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSArray *sourceArr;
@end

@implementation QKChooseController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSArray *arr = [QKReasouce projectJson:@"chooseData"];
    NSMutableArray *mutArr = [NSMutableArray array];
    for (NSUInteger index = 0; index < [arr count]; index ++)
    {
        NSArray *sectionArr = arr[index];
        NSArray *modelArr = [QKDanChooseModel mj_objectArrayWithKeyValuesArray:sectionArr];
        [mutArr addObject:modelArr];
    }
    
    self.sourceArr = [NSArray arrayWithArray:mutArr];
    
    UINib *nib = [UINib nibWithNibName:@"QKDanXuanCell"
                                bundle:nil];
    [self.tableView registerNib:nib
         forCellReuseIdentifier:@"DanXuanCell"];
    
    self.tableView.rowHeight = 44;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.sourceArr count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = self.sourceArr[section];
    return [arr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QKDanXuanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DanXuanCell"];
    QKDanChooseModel *model = self.sourceArr[indexPath.section][indexPath.row];
    cell.model = model;
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    QKDanChooseModel *model = self.sourceArr[section][0];
    
    return  model.section_name;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     QKDanChooseModel *model = self.sourceArr[indexPath.section][indexPath.row];
    if (model.choosed)
        return;

    NSArray *arr = self.sourceArr[indexPath.section];
    [arr makeObjectsPerformSelector:@selector(upDateChooseState:)
                         withObject:@0];
    
    model.choosed = YES;
    
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(indexPath.section, 1)] withRowAnimation:UITableViewRowAnimationNone];
    
}
@end

相关文章

网友评论

    本文标题:单选

    本文链接:https://www.haomeiwen.com/subject/gjdcjttx.html