diff --git a/AMTagListView/AMTagListView.h b/AMTagListView/AMTagListView.h index c902f7c..ab43397 100644 --- a/AMTagListView/AMTagListView.h +++ b/AMTagListView/AMTagListView.h @@ -19,6 +19,14 @@ typedef void (^AMTagListViewTapHandler)(AMTagView*); * ----------------------------------------------------------------------------- */ + +/** +* Add tag index +* +* @param index - Tag index +*/ +- (void)addTagIndex:(NSInteger)index; + /** Add a new tag * * Adds a new tag to the scroll view. @@ -35,6 +43,14 @@ typedef void (^AMTagListViewTapHandler)(AMTagView*); */ - (void)addTags:(NSArray*)array; +/** + * Add a multiple tags and indexs + * + * @param array - array An array of strings + * @param indexs - array tags index + */ +- (void)addTags:(NSArray*)array setIndexs:(NSArray *)indexs; + /** Add a specific tag to the tag scroll view * * @param tagView An AMTagView instance diff --git a/AMTagListView/AMTagListView.m b/AMTagListView/AMTagListView.m index f1934a3..4480edd 100644 --- a/AMTagListView/AMTagListView.m +++ b/AMTagListView/AMTagListView.m @@ -14,6 +14,8 @@ @interface AMTagListView () @property (nonatomic, strong) id orientationNotification; @property (nonatomic, strong) id tagNotification; +@property (nonatomic) NSInteger tagIndex; + @end @implementation AMTagListView @@ -61,6 +63,10 @@ - (void)setTapHandler:(AMTagListViewTapHandler)tapHandler _tapHandler = tapHandler; } +- (void)addTagIndex:(NSInteger)index { + _tagIndex = index; +} + - (void)addTag:(NSString*)text { UIFont* font = [[AMTagView appearance] textFont] ? [[AMTagView appearance] textFont] : kDefaultFont; @@ -74,6 +80,7 @@ - (void)addTag:(NSString*)text AMTagView* tagView = [[AMTagView alloc] initWithFrame:(CGRect){0, 0, size.width, size.height}]; [tagView setupWithText:text]; + [tagView setTag:_tagIndex]; [self.tags addObject:tagView]; [self rearrangeTags]; @@ -91,6 +98,7 @@ - (void)addTagView:(AMTagView *)tagView size.width = MIN(size.width, self.frame.size.width - self.marginX * 2); tagView.frame = (CGRect){0, 0, size.width, size.height}; + [tagView setTag:_tagIndex]; [self.tags addObject:tagView]; [self rearrangeTags]; @@ -132,6 +140,13 @@ - (void)rearrangeTags [self setContentSize:(CGSize){self.frame.size.width, maxY + size.height +self.marginY}]; } +- (void)addTags:(NSArray*)array setIndexs:(NSArray *)indexs { + [indexs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { + _tagIndex = [obj integerValue]; + [self addTag:[array objectAtIndex:idx]]; + }]; +} + - (void)addTags:(NSArray*)array { for (NSString* text in array) { diff --git a/TagListViewDemo/TagListViewDemo/AMViewController.m b/TagListViewDemo/TagListViewDemo/AMViewController.m index d2f5eae..45789be 100644 --- a/TagListViewDemo/TagListViewDemo/AMViewController.m +++ b/TagListViewDemo/TagListViewDemo/AMViewController.m @@ -40,9 +40,13 @@ - (void)viewDidLoad [self.tagListView addTag:@"something"]; [self.tagListView addTag:@"long tag is long"]; [self.tagListView addTag:@"hi there"]; + +// [[self tagListView] addTags:@[@"Hello", @"Hello people", @"Good"] setIndexs:@[@100, @200, @300]]; __weak AMViewController* weakSelf = self; [self.tagListView setTapHandler:^(AMTagView *view) { +// NSLog(@"%@", [view tagText]); +// NSLog(@"%d", view.tag); weakSelf.selection = view; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Delete" message:[NSString stringWithFormat:@"Delete %@?", [view tagText]]