自定义--问题答案展示控件

1:该控件可根据文字的多少自动布局且文字居中;

2:该控件包含支持多选和单选。暂不支持主观题目(包括问答);

3:该控件和UITableView一样简单易用。

4:内容涉及类库的编译和生成以及资源文件.bundle文件生成。

5:如果谁想要看源码,请留言!近期会在Github上传。

Demo目录机构图:

话不多说,展示主要代码:(.a 在以后的博客中会开放出来)

 1 //
 2 //  HRPTableView.h
 3 //  HRPTableView
 4 //
 5 //  Created by Hao Runpeng on 14-8-18.
 6 //  Copyright (c) 2014年 Hao Runpeng. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 @protocol HRPTableViewDataSource;
12 @protocol HRPtableViewDelegate;
13 
14 @interface HRPTableView : UIView
15 
16 @property (nonatomic) UIFont *questionFont;
17 @property (nonatomic) UIFont *optionFont;
18 
19 @property (nonatomic,weak) id<HRPtableViewDelegate>   delegate;
20 @property (nonatomic,weak) id<HRPTableViewDataSource> dataSource;
21 
22 - (instancetype)initWithFrame:(CGRect)frame;
23 
24 @end
25 
26 @protocol HRPTableViewDataSource <NSObject>
27 
28 @required
29 
30 - (NSInteger)numberOfQuestionsForTableView:(HRPTableView *)tableView;
31 - (NSString *)tableView:(HRPTableView *)tableView titleForQuestionAtIndex:(NSInteger)index;
32 - (NSInteger)tableView:(HRPTableView *)tableView numberOfOptionsForQuestionAtIndex:(NSInteger)index;
33 - (NSString *)tableView:(HRPTableView *)tableView titleforOptionAtIndex:(NSInteger)optionIndex forQuestionAtIndex:(NSInteger)questionIndex;
34 
35 - (BOOL)tableView:(HRPTableView *)tableView allowsMultipleSelectionForQuestionAtIndex:(NSInteger)index;
36 
37 @end
38 
39 @protocol HRPtableViewDelegate <NSObject>
40 
41 @required
42 
43 - (void)tableView:(HRPTableView *)tableView didSelectOptionAtIndex:(NSInteger)optionIndex forQuestionAtIndex:(NSInteger)questionIndex;
44 
45 - (void)tableView:(HRPTableView *)tableView didDeselectOptionAtIndex:(NSInteger)optionIndex forQuestionAtIndex:(NSInteger)questionIndex;
46 
47 @end
View Code

QuestionEntities model 如下 ,格式不固定 可以根据自己的需要自行更改

 1 //
 2 //  QuestionEntities.h
 3 //  Call
 4 //
 5 //  Created by Hao Runpeng on 14-8-15.
 6 //  Copyright (c) 2014年 Hao Runpeng. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 typedef NS_ENUM(NSInteger, QuestionType) {
12   QuestionTypeSingleChoice   = 0, // 单选题
13   QuestionTypeMultipleChoice = 1, // 多选题
14   QuestionTypeSubjective     = 2, // 主观题
15 };
16 
17 @interface Question : NSObject
18 
19 @property NSString     *Id;
20 @property NSString     *content;
21 @property QuestionType type;
22 @property NSArray      *options;
23 @property NSArray      *optionsResult;
24 
25 @end
26 
27 @interface Option : NSObject
28 
29 @property NSString *Id;
30 @property NSString *questionId;
31 @property NSString *content;
32 @property BOOL      isCorrect;
33 
34 @end
35 
36 
37 /*.m*/
38 //
39 //  QuestionEntities.m
40 //  Call
41 //
42 //  Created by Hao Runpeng on 14-8-15.
43 //  Copyright (c) 2014年 Hao Runpeng. All rights reserved.
44 //
45 
46 #import "QuestionEntities.h"
47 
48 @implementation Question
49 
50 @end
51 
52 @implementation Option
53 
54 @end
View Code

RootViewController.m 主要代码如下:

  1 //
  2 //  RootViewController.m
  3 //  HRPTableViewSample
  4 //
  5 //  Created by Hao Runpeng on 14-8-22.
  6 //  Copyright (c) 2014年 Hao Runpeng. All rights reserved.
  7 //
  8 
  9 #import "RootViewController.h"
 10 #import "HRPTableView.h"
 11 #import "QuestionEntities.h"
 12 
 13 @interface RootViewController ()
 14 <HRPtableViewDelegate,HRPTableViewDataSource>
 15 
 16 @property (nonatomic,strong) HRPTableView *tableView;
 17 @property (nonatomic)        NSArray      *questions;
 18 
 19 @end
 20 
 21 @implementation RootViewController
 22 
 23 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 24 {
 25   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 26   if (self) {
 27 
 28   }
 29   return self;
 30 }
 31 
 32 - (void)viewDidLoad
 33 {
 34   [super viewDidLoad];
 35   [self setUpTableDataSource];
 36   
 37   _tableView = [[HRPTableView alloc] initWithFrame:CGRectMake(100, 20, 824, 748)];
 38   _tableView.questionFont = [UIFont systemFontOfSize:16];
 39   _tableView.optionFont   = [UIFont systemFontOfSize:14];
 40   _tableView.dataSource   = self;
 41   _tableView.delegate     = self;
 42   
 43   [self.view addSubview:_tableView];
 44 }
 45 
 46 
 47 - (void)setUpTableDataSource
 48 {
 49   Option *option1 = [[Option alloc] init];
 50   option1.Id         = @"1";
 51   option1.content    = @"";
 52   option1.isCorrect  = NO;
 53   option1.questionId = @"1";
 54   
 55   Option *option2 = [[Option alloc] init];
 56   option2.Id         = @"2";
 57   option2.content    = @"";
 58   option2.isCorrect  = NO;
 59   option2.questionId = @"1";
 60   
 61   Option *option3 = [[Option alloc] init];
 62   option3.Id         = @"3";
 63   option3.content    = @"本身具有过敏体质请输入新鲜血液和血浆!请输入新鲜血液和血浆!请输入新鲜血液和血浆!请输入新鲜血液和血浆!请输入新鲜血液和血浆!请输入新鲜血液和血浆!本身具有过敏体质请输入新鲜血液和血浆!请输入新鲜血液和血浆!请输入新鲜血液和血浆!请输入新鲜血液和血浆!请输入新鲜血液和血浆!请输入新鲜血液和血浆!本身具有过敏体质请输入新鲜血液和血浆!请输入新鲜血液和血浆!请输入新鲜血液和血浆!请输入新鲜血液和血浆!请输入新鲜血液和血浆!请输入新鲜血液和血浆!请输入新鲜血液和血浆!请输入新鲜血液和血浆!请输入新鲜血液和血浆!2请输入新鲜血液和血浆!请输入新鲜血液和血浆!0请输入新鲜血液和血浆0";
 64   option3.isCorrect  = NO;
 65   option3.questionId = @"1";
 66   
 67   NSArray *options = @[option1,option2,option3];
 68   
 69   Question *question1 = [[Question alloc] init];
 70   question1.Id      = @"1";
 71   question1.type    = QuestionTypeSingleChoice;
 72   question1.content = @"Q1:你认为印发长期过敏不愈的主要原因是什么?你认为印发长期过敏不愈的主要原因是什么?你认为印发长期过敏不愈的主要原因是什么?你认为印发长期过敏不愈的主要原因是什么?你认为印发长期过敏不愈的主要原因是什么?你认为印发长期过敏不愈的主要原因是什么?你认为印发长期过敏不愈的主要原因是什么!";
 73   question1.options = options;
 74   
 75   Question *question2 = [[Question alloc] init];
 76   question2.Id      = @"2";
 77   question2.type    = QuestionTypeSingleChoice;
 78   question2.content = @"Q2:过敏是不是长期疾病?";
 79   question2.options = options;
 80   
 81   Question *question3 = [[Question alloc] init];
 82   question3.Id      = @"3";
 83   question3.type    = QuestionTypeSingleChoice;
 84   question3.content = @"Q3:你认为如何治疗过敏才是最佳方式?";
 85   question3.options = options;
 86   
 87   Question *question4 = [[Question alloc] init];
 88   question4.Id      = @"4";
 89   question4.type    = QuestionTypeMultipleChoice;
 90   question4.content = @"Q4:过敏是否需要长期治疗";
 91   question4.options = options;
 92   
 93   self.questions = @[question1,question2,question3,question4];
 94 }
 95 
 96 #pragma mark -
 97 #pragma mark HRPTableView DataSource & Delegate
 98 
 99 - (NSInteger) numberOfQuestionsForTableView:(HRPTableView *)tableView
100 {
101   return self.questions.count;
102 }
103 
104 - (NSString *)tableView:(HRPTableView *)tableView
105 titleForQuestionAtIndex:(NSInteger)index
106 {
107   Question *question = self.questions[index];
108   return question.content;
109 }
110 
111 - (NSInteger) tableView:(HRPTableView *)tableView numberOfOptionsForQuestionAtIndex:(NSInteger)index
112 {
113   Question *question = self.questions[index];
114   return question.options.count;
115 }
116 
117 - (NSString *)tableView:(HRPTableView *)tableView
118   titleforOptionAtIndex:(NSInteger)optionIndex
119      forQuestionAtIndex:(NSInteger)questionIndex
120 {
121   Question *question = self.questions[questionIndex];
122   Option   *option   = question.options[optionIndex];
123   return option.content;
124 }
125 
126 - (BOOL)tableView:(HRPTableView *)tableView allowsMultipleSelectionForQuestionAtIndex:(NSInteger)index
127 {
128   Question *question = self.questions[index];
129   return question.type == QuestionTypeMultipleChoice;
130 }
131 
132 - (void)tableView:(HRPTableView *)tableView
133     didSelectOptionAtIndex:(NSInteger)optionIndex
134         forQuestionAtIndex:(NSInteger)questionIndex
135 {
136   NSLog(@"select:%d -- %d",questionIndex,optionIndex);
137 }
138 
139 - (void)tableView:(HRPTableView *)tableView
140     didDeselectOptionAtIndex:(NSInteger)optionIndex
141           forQuestionAtIndex:(NSInteger)questionIndex
142 {
143   NSLog(@"Deselect:%d -- %d",questionIndex,optionIndex);
144 }
145 
146 - (void)didReceiveMemoryWarning
147 {
148   [super didReceiveMemoryWarning];
149 }
150 
151 @end
View Code

运行效果如下:

 
 
 
原文地址:https://www.cnblogs.com/xiaohaoweiye/p/4368715.html