forked from escoz/QuickDialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QSelectSection.m
131 lines (107 loc) · 3.38 KB
/
QSelectSection.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// QSelectSection.m
// QuickDialog
//
// Created by HiveHicks on 23.03.12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "QSelectSection.h"
#import "QuickDialog.h"
@implementation QSelectSection {
}
@synthesize selectedIndexes = _selected;
@synthesize multipleAllowed = _multipleAllowed;
@synthesize onSelected = _onSelected;
- (id)init {
self = [super init];
if (self) {
self.selectedIndexes = [@[] mutableCopy];
self.multipleAllowed = NO;
self.deselectAllowed = NO;
}
return self;
}
- (QSelectSection *)initWithItems:(NSArray *)stringArray selectedIndexes:(NSArray *)selected
{
return [self initWithItems:stringArray selectedIndexes:selected title:nil];
}
- (QSelectSection *)initWithItems:(NSArray *)stringArray selectedIndexes:(NSArray *)selected title:(NSString *)title
{
if (self = [super initWithTitle:title])
{
_items = [stringArray mutableCopy];
_selected = selected ? [selected mutableCopy] : [NSMutableArray array];
_multipleAllowed = (_selected.count > 1);
[self createElements];
}
return self;
}
- (QSelectSection *)initWithItems:(NSArray *)items selectedItems:(NSArray *)selectedItems title:(NSString *)title
{
NSMutableArray *selectedIndexes = [NSMutableArray array];
for (id item in selectedItems) {
NSUInteger index = [items indexOfObject:item];
if (index != NSNotFound) {
[selectedIndexes addObject:[NSNumber numberWithUnsignedInteger:index]];
}
}
return [self initWithItems:items selectedIndexes:selectedIndexes title:title];
}
- (QSelectSection *)initWithItems:(NSArray *)stringArray selected:(NSUInteger)selected
{
return [self initWithItems:stringArray selected:selected title:nil];
}
- (QSelectSection *)initWithItems:(NSArray *)stringArray selected:(NSUInteger)selected title:(NSString *)title
{
return [self initWithItems:stringArray
selectedIndexes:[NSArray arrayWithObject:[NSNumber numberWithUnsignedInteger:selected]]
title:title];
}
- (NSArray *)items
{
return _items;
}
- (void)setItems:(NSArray *)items
{
_items = [items mutableCopy];
self.elements = nil;
[self createElements];
}
- (NSArray *)selectedItems
{
NSMutableArray *selectedItems = [NSMutableArray array];
for (NSNumber *index in _selected) {
[selectedItems addObject:[_items objectAtIndex:[index unsignedIntegerValue]]];
}
return selectedItems;
}
- (void)createElements
{
for (NSUInteger i = 0; i < [_items count]; i++) {
[self addElement:[[QSelectItemElement alloc] initWithIndex:i selectSection:self]];
}
}
- (void)addElement:(QElement *)element {
[super addElement:element];
if ([element isKindOfClass:[QSelectItemElement class]]){
((QSelectItemElement *)element).selectSection = self;
((QSelectItemElement *)element).index = self.elements.count-1;
}
}
- (void)addOption:(NSString *)option
{
[self insertOption:option atIndex:_items.count];
}
- (void)insertOption:(NSString *)option atIndex:(NSUInteger)index
{
[_items insertObject:option atIndex:index];
QSelectItemElement *element = [[QSelectItemElement alloc] initWithIndex:index selectSection:self];
[self insertElement:element atIndex:index];
}
- (void)fetchValueIntoObject:(id)obj
{
if (_key) {
[obj setValue:_selected forKey:_key];
}
}
@end