2016-03-21 8 views
-1

私は大量の8k + Line FeedViewControllerをパックしていましたが、いつでも私は何十ものviewcontrllersを変更しなければならないステータスの外観を含む何かを変更したいので、リファクタリングをMVCタイプの標準に従おうと決めました。これは本当に間違っている.....私はだけではなく、12個のファイルのセルクラスを変更することができるように、私はtableviewcellsに機能のほとんどを入れて、今イム公聴会「あなたはダムあなたの閲覧数を維持すべきか?MVCはIOSのコンテキストでも何を意味しますか?

// 
// TriCornerFeedCell.m 

// 

// 
#import "NSDate+TimeAgo.h" 
#import "TriCornerFeedCell.h" 
#import "AFNetworking.h" 


@implementation TriCornerFeedCell 

-(void)refreshGestureRecognizers{ 

    while (self.gestureRecognizers.count) { 
     [self removeGestureRecognizer:[self.gestureRecognizers objectAtIndex:0]]; 
    } 
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processDoubleTap:)]; 
    doubleTap.numberOfTapsRequired = 2; 
    [self addGestureRecognizer:doubleTap]; 

    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
              initWithTarget:self action:@selector(handleLongPress:)]; 
    lpgr.minimumPressDuration = 1.3; //seconds 
    lpgr.delegate = self; 
    [self addGestureRecognizer:lpgr]; 
} 




-(void)refreshCell{ 

    _sdmanager = [SDWebImageManager sharedManager]; 


//Set Name Label 
    self.NameLabel.text=[NSString stringWithFormat:@"%@ %@",self.statusObject.first_name,self.statusObject.last_name]; 

//Set Message Label 
    if(self.statusObject.message!=nil){ 
     self.StatusLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink; 
     self.StatusLabel.text = self.statusObject.message; 
     self.StatusLabel.delegate = self; 
     [self checkForShoutoutsAndHashtags]; 
     self.StatusLabel.lineBreakMode=0; 
     self.StatusLabel.numberOfLines=0; 
     //Cell.StatusLabel.text=[tempDictionary objectForKey:@"message"]; 
     [self.StatusLabel sizeToFit]; 

    } 

//Set Date Label 
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:self.statusObject.created]; 
    self.timeLabel.text=[date timeAgo]; 

//Set Bump Views 
    if(self.statusObject.isBumped){ 
     [self.bumpCornerBG setBackgroundImage:[UIImage imageNamed:@"corner_bump_yellow.png"] forState:UIControlStateNormal]; 

    }else{ 
     [self.bumpCornerBG setBackgroundImage:[UIImage imageNamed:@"corner_bump_grey.png"] forState:UIControlStateNormal]; 
    } 

    self.bumpCount.text=[@(self.statusObject.bump_count) stringValue]; 
    [self.bumpClearViewBtn addTarget:self action:@selector(bump:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.bumpCornerBG addTarget:self action:@selector(bump:) forControlEvents:UIControlEventTouchUpInside]; 


//Set Comment Related Views 


    if(self.statusObject.comment_count>0){ 
     NSLog(@"COMMENT_COUNT:%d",self.statusObject.comment_count); 
     [self.commentCount setHidden:NO]; 
     [self.miniCommentColorCircle setHidden:NO]; 
     self.commentCount.text=[@(self.statusObject.comment_count) stringValue]; 
    }else{ 
     [self.commentCount setHidden:YES]; 
     [self.miniCommentColorCircle setHidden:YES]; 
    } 

//Set Follow Btn Views 
    [self.followBtn.titleLabel setFont:[UIFont fontWithName:@"BloggerSans" size:14]]; 
    [self.followBtn removeFromSuperview]; 
    if(!self.statusObject.isFollowing){ 

     [self.followBtn removeFromSuperview]; 
     UIButton *followBtn= [[UIButton alloc] initWithFrame:CGRectMake(235, 16, 75, 25)]; 
     [followBtn setImage:nil forState:UIControlStateNormal]; 
     followBtn.layer.cornerRadius =2; // this value vary as per your desire 
     [followBtn.titleLabel setFont:[UIFont fontWithName:@"BloggerSans" size:14.0f]]; 
     followBtn.titleEdgeInsets = UIEdgeInsetsMake(3, 0, 0, 0); 
     followBtn.clipsToBounds = YES; 
     [followBtn setTitleColor:[self colorWithHexString:@"ff68a8"] forState:UIControlStateNormal]; 
     followBtn.layer.borderWidth=1.5f; 
     followBtn.layer.borderColor=[[self colorWithHexString:@"ff68a8"] CGColor]; 
     [followBtn setTitle:@"Follow" forState:UIControlStateNormal]; 
     [followBtn addTarget:self action:@selector(follow) forControlEvents:UIControlEventTouchUpInside]; 
     self.followBtn=followBtn; 
     [self addSubview:self.followBtn]; 

    } 


//Reset icon to default in case default==nill 
    [self bringSubviewToFront:self.DefaultImgBorder]; 
    [self bringSubviewToFront:self.DefaultImgBtn]; 
    UIImage *noDefault = [UIImage imageNamed:@"female_mini_no_default_icon.png"]; 
    [self.DefaultImgBtn setBackgroundImage:noDefault forState:UIControlStateNormal]; 

//Attempt to create hexagon cropped default photo 
    UIImage *hex_img = [UIImage imageNamed:@"big_hex_thumb.png"]; 
    UIImage *tan=[UIImage imageNamed:@"feed_grey.png"]; 
    UIImage *finalBG= [self maskImage:tan withMask:hex_img]; 
    [self.DefaultImgBorder setImage:finalBG]; 
    NSURL *thumb_url=[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",s3thumbURL,self.statusObject.thumb_img]]; 
    @autoreleasepool { 
     [_sdmanager downloadWithURL:thumb_url 
          options:0 
          progress:^(NSInteger receivedSize, NSInteger expectedSize){}completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished){ 
      if (image) 
      { 
       UIImage *thumbimg = image; 
       UIImage *hex_img = [UIImage imageNamed:@"big_hex_thumb.png"]; 
       UIImage *noDefault = [UIImage imageNamed:@"female_mini_no_default_icon.png"]; 
       [self.DefaultImgBtn setBackgroundImage:noDefault forState:UIControlStateNormal]; 

       UIImage *finalImg= [self maskImage:thumbimg withMask:hex_img]; 
       [self.DefaultImgBtn setBackgroundImage:finalImg forState:UIControlStateNormal ]; 
      } 
     }]; 
    } 
    [self.DefaultImgBtn removeTarget:nil 
           action:NULL 
        forControlEvents:UIControlEventAllEvents]; 
    [self.DefaultImgBtn addTarget:self action:@selector(viewProfile:) forControlEvents:UIControlEventTouchUpInside]; 


//Misc Stuff 
    [self setSelectionStyle:UITableViewCellSelectionStyleNone]; 

} 

#pragma mark - Interaction Methods 

-(void)follow{ 


    [self.followSpinner removeFromSuperview]; 
    [self.followBtn removeFromSuperview]; 
    self.followSpinner = [[RTSpinKitView alloc] initWithStyle:RTSpinKitViewStyleFadingCircleAlt color: [self colorWithHexString:@"b4b4b2"] spinnerSize:25]; 
    [self.followSpinner setFrame:CGRectMake(260,15,30,30)]; 
    self.spinner.tag=9; 
    [self addSubview:self.followSpinner]; 
    [self.statusObject follow]; 
} 
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer 
{ 

    if (self.indexPath != nil && gestureRecognizer.state == UIGestureRecognizerStateBegan) { 


     NSString*loggedin_uid = [[NSUserDefaults standardUserDefaults] objectForKey:@"uid"]; 


     if([self.statusObject.IMPORT_SOURCE isEqualToString:@"INSTAGRAM"] || [self.statusObject.IMPORT_SOURCE isEqualToString:@"TWITTER"]){ 

      if([loggedin_uid isEqualToString:@"1"] || [loggedin_uid isEqualToString:@"2"]){ 

       [self.delegate showImportAdminControls:self.indexPath]; 
      } 

     }else{ 

      if([loggedin_uid isEqualToString:@"1"] || [loggedin_uid isEqualToString:@"2"]){ 

       [self.delegate showAdminControls:self.indexPath]; 

      }else if ([self.statusObject.uid isEqualToString:loggedin_uid]){ 

       [self.delegate deletePostAlert:self.indexPath]; 

      } 
     } 

    } 
} 
- (void) processDoubleTap:(UITapGestureRecognizer *)sender 
{ 


    if (sender.state == UIGestureRecognizerStateEnded) 
    { 
     if (self.indexPath) 
     { 
      self.statusObject.indexPath=self.indexPath; 

      if(!self.statusObject.isBumped){ 


       int value = self.statusObject.bump_count+1; 
       self.statusObject.isBumped=YES; 
       self.statusObject.bump_count=value; 
       [self.bumpCount setText:[@(value) stringValue]]; 
       [self.bumpCornerBG setBackgroundImage:[UIImage imageNamed:@"corner_bump_yellow.png"] forState:UIControlStateNormal]; 
       UIImageView *bumpBlowUpIcon=[[UIImageView alloc]initWithFrame:CGRectMake((self.frame.size.width/2)-5, (self.frame.size.height/2)-5, 10, 10)]; 
       [bumpBlowUpIcon setImage:[UIImage imageNamed:@"heart_yellow.png"]]; 
       bumpBlowUpIcon.alpha=.2; 
       [self addSubview:bumpBlowUpIcon]; 

       [UIView animateWithDuration:.8 delay:0 usingSpringWithDamping:.3 initialSpringVelocity:.3 options:UIViewAnimationOptionCurveEaseOut animations:^{ 

        [bumpBlowUpIcon setFrame:CGRectMake((self.frame.size.width/2)-(self.frame.size.height/2), 0, self.frame.size.height, self.frame.size.height)]; 
        bumpBlowUpIcon.alpha=.5; 

       }completion:^(BOOL finished) { 

        [bumpBlowUpIcon removeFromSuperview]; 
       }]; 


      }else{ 

       int value = self.statusObject.bump_count-1; 
       self.statusObject.isBumped=NO; 
       self.statusObject.bump_count=value; 
       [self.bumpCount setText:[@(value) stringValue]]; 
       [self.bumpCornerBG setBackgroundImage:[UIImage imageNamed:@"corner_bump_grey.png"] forState:UIControlStateNormal]; 
      } 



      [self.statusObject bump]; 





     } 
    } 

} 




-(void)viewProfile{ 



} 

-(void)viewHashtag{ 



} 



#pragma mark - Misc Methods 




- (UIImage*) maskImage:(UIImage *) image withMask:(UIImage *) mask 
{ 
    CGImageRef imageReference = image.CGImage; 
    CGImageRef maskReference = mask.CGImage; 

    CGImageRef imageMask = CGImageMaskCreate(CGImageGetWidth(maskReference), 
              CGImageGetHeight(maskReference), 
              CGImageGetBitsPerComponent(maskReference), 
              CGImageGetBitsPerPixel(maskReference), 
              CGImageGetBytesPerRow(maskReference), 
              CGImageGetDataProvider(maskReference), 
              NULL, // Decode is null 
              YES // Should interpolate 
              ); 

    CGImageRef maskedReference = CGImageCreateWithMask(imageReference, imageMask); 
    CGImageRelease(imageMask); 

    UIImage *maskedImage = [UIImage imageWithCGImage:maskedReference]; 
    CGImageRelease(maskedReference); 

    return maskedImage; 
} 

-(UIColor*)colorWithHexString:(NSString*)hex 
{ 
    NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString]; 

    // String should be 6 or 8 characters 
    if ([cString length] < 6) return [UIColor grayColor]; 

    // strip 0X if it appears 
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2]; 

    if ([cString length] != 6) return [UIColor grayColor]; 

    // Separate into r, g, b substrings 
    NSRange range; 
    range.location = 0; 
    range.length = 2; 
    NSString *rString = [cString substringWithRange:range]; 

    range.location = 2; 
    NSString *gString = [cString substringWithRange:range]; 

    range.location = 4; 
    NSString *bString = [cString substringWithRange:range]; 

    // Scan values 
    unsigned int r, g, b; 
    [[NSScanner scannerWithString:rString] scanHexInt:&r]; 
    [[NSScanner scannerWithString:gString] scanHexInt:&g]; 
    [[NSScanner scannerWithString:bString] scanHexInt:&b]; 

    return [UIColor colorWithRed:((float) r/255.0f) 
          green:((float) g/255.0f) 
          blue:((float) b/255.0f) 
          alpha:1.0f]; 
} 

@end 
+0

MVCパターンに固執したいときは、ビューをできるだけダムにしておく必要がありますが、それは正しいことです。最良の場合、ビジネスロジックを含むべきではありません。 – Alex

+0

私は質問にその理論を置くより具体的な質問を読んでください – ChuckKelly

+0

"...ビジネスロジックを含んでいない"は、彼らもビジネス_オブジェクト(モデル)に関する**何も**知ってはいけない**ことを意味します。 'self.StatusLabel.text = self.statusObject.message;'の 'statusObject'がデータ転送オブジェクト以外のものであれば、セルが自分の値を取り出すことはできません。 – Alex

答えて

0

私はすべてのコメントを適切な回答にマージしようとしました。

MVCパターンに固執したいときは、ビューをできるだけダムにしてください(ビュー関連およびビジネス関連のコンポーネント彼らはビジネスについて何も知ってはならないはずであることを意味するオブジェクト(モデル)。あなたがデータ転送オブジェクト以外の何かをしているのであれば、セルに自分の値を取り込ませるのは良い考えではありません。なぜなら、これらのセルはあなたのモデル構造について知識を持つ必要があるからです。

通常、各ビューには独自のビューコントローラがありますが、もちろん必要な機能を備えたビューコントローラを作成することはできます。

たとえば、一部のビューコントローラにデータが必要な場合、そのデータを提供する別の(ビュー関連の)コンポーネントを導入することができます(たとえば、セルの作成と返却による)。このコンポーネントにはモデルデータ(またはデータ転送オブジェクト)が供給され、これらの情報を必要とするすべてのView Controllerで使用できる構造体が返されます。

関連する問題