ios - UITableView Subclass with Loading Activity Indicator -
my application has several uitableviews should display loading indicator while i'm fetching info populate table. i'm trying come smart way of implementing this.
should subclass uitableview, default overlay tableview view has activity indicator , label? assign every uitableview in storyboard subclass.
but have 2 concerns:
i've read it's not thought subclass uitableview? there improve paradigm doing this? if subclass uitableview, how started adding custom view overlay programmatically?
you can utilize solution:
uiviewsindicatorspull.h
#import <foundation/foundation.h> @interface uiviewsindicatorcontainer : nsobject @property (nonatomic, weak) uiview *view; @property (nonatomic, strong)uiactivityindicatorview *indicator; @end @interface uiviewsindicatorspull : nsobject + (instancetype)instance; - (uiactivityindicatorview*)indicatorforviewobject:(uiview*)object; - (uiactivityindicatorview*)indicatorforviewobject:(uiview*)object style:(uiactivityindicatorviewstyle)style; - (void)removeindicatorforviewobject:(uiview*)object; @end
uiviewsindicatorspull.m
#import "uiviewsindicatorspull.h" @implementation uiviewsindicatorspull { nsmutablearray *indicators; } + (instancetype)instance { static uiviewsindicatorspull *sharedinstance; static dispatch_once_t oncetoken; dispatch_once(&oncetoken, ^{ sharedinstance = [uiviewsindicatorspull new]; }); homecoming sharedinstance; } - (uiactivityindicatorview*)indicatorforviewobject:(uiview*)object { homecoming [self indicatorforviewobject:object style:uiactivityindicatorviewstylegray]; } - (uiactivityindicatorview*)indicatorforviewobject:(uiview*)object style:(uiactivityindicatorviewstyle)style { if (indicators == nil) { indicators = [nsmutablearray array]; } uiactivityindicatorview *result; (uiviewsindicatorcontainer *entity in indicators){ if (entity.view == object) { result = (uiactivityindicatorview*)entity.indicator; break; } } if (result == nil) { result = [[uiactivityindicatorview alloc] initwithactivityindicatorstyle:style]; [object addsubview:result]; uiviewsindicatorcontainer *entity = [[uiviewsindicatorcontainer alloc] init]; entity.view = object; entity.indicator = (uiactivityindicatorview*)result; [indicators addobject:entity]; } homecoming result; } - (void)removeindicatorforviewobject:(uiview*)object { (uiviewsindicatorcontainer *entity in indicators){ if (entity.view == object) { uiactivityindicatorview *indicator = entity.indicator; [indicator stopanimating]; [indicator removefromsuperview]; [indicators removeobject:entity]; break; } } } @end @implementation uiviewsindicatorcontainer @end
uiview+additions.h
#import <uikit/uikit.h> #import "uiviewsindicatorspull.h" #define uiviewautoresizingall uiviewautoresizingflexibleheight|uiviewautoresizingflexiblewidth @interface uiview (additions) //process indicator - (void)showprocessindicator; - (void)showprocessindicatorwithstyle:(uiactivityindicatorviewstyle)style; - (void)showprocessindicatorwithalignment:(uiviewcontentmode)mode; - (void)showprocessindicatorwithalignment:(uiviewcontentmode)mode style:(uiactivityindicatorviewstyle)style; - (void)hideprocessindicator; - (bool)isprocessindicatorpresented; @end
uiview+additions.m
#import "uiview+additions.h" @implementation uiview (uiview_additions) - (void)showprocessindicatorwithstyle:(uiactivityindicatorviewstyle)style { [self showprocessindicatorwithalignment:uiviewcontentmodecenter style:style]; } - (void)showprocessindicator { [self showprocessindicatorwithalignment:uiviewcontentmodecenter style:uiactivityindicatorviewstylegray]; } - (void)showprocessindicatorwithalignment:(uiviewcontentmode)mode style:(uiactivityindicatorviewstyle)style { uiactivityindicatorview *indicator = (uiactivityindicatorview*)[[uiviewsindicatorspull instance] indicatorforviewobject:self style:style]; [self positionindicator:indicator andalignment:mode]; [indicator startanimating]; [self.superview addsubview:indicator]; self.alpha = 0; } - (void)showprocessindicatorwithalignment:(uiviewcontentmode)mode { [self showprocessindicatorwithalignment:mode style:uiactivityindicatorviewstylegray]; } - (void)positionindicator:(uiactivityindicatorview*)indicator andalignment:(uiviewcontentmode)mode { cgrect frame = indicator.frame; switch (mode) { case uiviewcontentmoderight: frame.origin.x = self.frame.origin.x + self.frame.size.width - frame.size.width; frame.origin.y = self.frame.origin.y + self.frame.size.height / 2 - frame.size.height / 2; indicator.autoresizingmask = uiviewautoresizingflexibleleftmargin | uiviewautoresizingflexibletopmargin | uiviewautoresizingflexiblebottommargin; break; case uiviewcontentmodeleft: frame.origin.x = self.frame.origin.x; frame.origin.y = self.frame.origin.y + self.frame.size.height / 2 - frame.size.height / 2; indicator.autoresizingmask = uiviewautoresizingflexiblerightmargin | uiviewautoresizingflexibletopmargin | uiviewautoresizingflexiblebottommargin; break; case uiviewcontentmodetop: frame.origin.x = self.frame.origin.x + self.frame.size.width / 2 - frame.size.width / 2; frame.origin.y = self.frame.origin.y; indicator.autoresizingmask = uiviewautoresizingflexiblebottommargin | uiviewautoresizingflexibleleftmargin | uiviewautoresizingflexiblerightmargin; break; case uiviewcontentmodebottom: frame.origin.x = self.frame.origin.x + self.frame.size.width / 2 - frame.size.width / 2; frame.origin.y = self.frame.origin.y + self.frame.size.height - frame.size.height; indicator.autoresizingmask = uiviewautoresizingflexibletopmargin | uiviewautoresizingflexibleleftmargin | uiviewautoresizingflexiblerightmargin; break; case uiviewcontentmodetopleft: frame.origin.x = self.frame.origin.x; frame.origin.y = self.frame.origin.y; indicator.autoresizingmask = uiviewautoresizingflexiblebottommargin | uiviewautoresizingflexiblerightmargin; break; case uiviewcontentmodetopright: frame.origin.x = self.frame.origin.x + self.frame.size.width - frame.size.width; frame.origin.y = self.frame.origin.y; indicator.autoresizingmask = uiviewautoresizingflexiblebottommargin | uiviewautoresizingflexibleleftmargin; break; case uiviewcontentmodebottomleft: frame.origin.x = self.frame.origin.x; frame.origin.y = self.frame.origin.y + self.frame.size.height - frame.size.height; indicator.autoresizingmask = uiviewautoresizingflexibletopmargin | uiviewautoresizingflexiblerightmargin; break; case uiviewcontentmodebottomright: frame.origin.x = self.frame.origin.x + self.frame.size.width - frame.size.width; frame.origin.y = self.frame.origin.y + self.frame.size.height - frame.size.height; indicator.autoresizingmask = uiviewautoresizingflexibletopmargin | uiviewautoresizingflexibleleftmargin; break; case uiviewcontentmodecenter: frame.origin.x = self.frame.origin.x + self.frame.size.width / 2 - frame.size.width / 2; frame.origin.y = self.frame.origin.y + self.frame.size.height / 2 - frame.size.height / 2; indicator.autoresizingmask = uiviewautoresizingflexiblebottommargin | uiviewautoresizingflexiblerightmargin | uiviewautoresizingflexibletopmargin | uiviewautoresizingflexibleleftmargin; break; default: frame.origin.x = self.frame.origin.x + self.frame.size.width / 2 - frame.size.width / 2; frame.origin.y = self.frame.origin.y + self.frame.size.height / 2 - frame.size.height / 2; indicator.autoresizingmask = uiviewautoresizingflexiblebottommargin | uiviewautoresizingflexiblerightmargin | uiviewautoresizingflexibletopmargin | uiviewautoresizingflexibleleftmargin; break; } indicator.frame = frame; } - (void)hideprocessindicator { uiactivityindicatorview *indicator = [[uiviewsindicatorspull instance] indicatorforviewobject:self]; [indicator stopanimating]; [indicator removefromsuperview]; self.alpha = 1.0; } - (bool)isprocessindicatorpresented { homecoming self.alpha == 0; } @end
add files project, , phone call cell:
[cell.contentview showprocessindicator]; [cell.contentview showprocessindicatorwithalignment:uiviewcontentmodecenter]; [cell.contentview showprocessindicatorwithalignment:uiviewcontentmodecenter style:uiactivityindicatorviewstylegray]; [cell.contentview hideprocessindicator];
ios uitableview coding-style subclass
No comments:
Post a Comment