MVC在IOS环境中到底意味着什么?
我曾经有一个大容量的8k+线路FeedViewController,每当我想改变任何涉及状态外观的东西时,我必须改变不少于12个ViewControllers,所以我决定尝试按照MVC类型的标准进行重构。我将大部分功能放在tableviewcells中,这样我就可以更改cell类而不是12个文件,现在我听到“你应该保持你的视图沉默……这真的错了吗?”MVC在IOS环境中到底意味着什么?,ios,objective-c,uitableview,model-view-controller,Ios,Objective C,Uitableview,Model View Controller,我曾经有一个大容量的8k+线路FeedViewController,每当我想改变任何涉及状态外观的东西时,我必须改变不少于12个ViewControllers,所以我决定尝试按照MVC类型的标准进行重构。我将大部分功能放在tableviewcells中,这样我就可以更改cell类而不是12个文件,现在我听到“你应该保持你的视图沉默……这真的错了吗?” // // TriCornerFeedCell.m // // #import "NSDate+TimeAgo.h" #import "Tr
//
// 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
//
//TriCornerFeedCell.m
//
//
#导入“NSDate+TimeAgo.h”
#导入“TriCornerFeedCell.h”
#导入“AFNetworking.h”
@三角形细胞的实现
-(无效)刷新标识识别器{
while(self.gestureignizers.count){
[self-RemovegeSetureRecognitor:[self.GestureRecognitors对象索引:0];
}
UITapGestureRecognizer*doubleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(processDoubleTap:)];
doubleTap.numberOfTapsRequired=2;
[自选地址识别器:双击];
UILongPressGestureRecognizer*lpgr=[[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration=1.3;//秒
lpgr.delegate=self;
[自选地址识别器:lpgr];
}
-(空)刷新单元格{
_sdmanager=[SDWebImageManager共享管理器];
//设置名称标签
self.namelab.text=[NSString stringWithFormat:@“%@%@”,self.statusObject.first_name,self.statusObject.last_name];
//设置消息标签
if(self.statusObject.message!=nil){
self.StatusLabel.enabledTextCheckingTypes=NSTextCheckingTypeLink;
self.StatusLabel.text=self.statusObject.message;
self.StatusLabel.delegate=self;
[自动检查首字母和标签];
self.StatusLabel.lineBreakMode=0;
self.StatusLabel.numberOfLines=0;
//Cell.StatusLabel.text=[tempDictionary objectForKey:@“message”];
[self.StatusLabel sizeToFit];
}
//设置日期标签
NSDate*date=[[NSDate alloc]initWithTimeIntervalSince1970:self.statusObject.created];
self.timeLabel.text=[date-timeAgo];
//设置凹凸视图
if(self.statusObject.isBumped){
[self.bumpCornerBG setBackgroundImage:[UIImage ImageName:@“corner\u bump\u yellow.png”]用于状态:UIControlStateNormal];
}否则{
[self.bumpCornerBG setBackgroundImage:[UIImage ImageName:@“corner\u bump\u grey.png”]用于状态:UIControlStateNormal];
}
self.bumpCount.text=[@(self.statusObject.bump_count)stringValue];
[self.bumpClearViewBtn addTarget:self action:@selector(bump:)for controlEvents:UIControlEventTouchUpInside];
[self.bumpCornerBG addTarget:self action:@selector(bump:)for controlEvents:UIControlEventTouchUpInside];
//设置与注释相关的视图
if(self.statusObject.comment\u count>0){
NSLog(@“注释计数:%d”,self.statusObject.COMMENT计数);
[self.commentCount setHidden:否];
[self.miniCommentColorCircle setHidden:否];
self.commentCount.text=[@(self.statusObject.comment_count)stringValue];
}否则{
[self.commentCount setHidden:是];
[self.miniCommentColorCircle setHidden:是];
}
//设置跟随Btn视图
[self.followBtn.titleLabel setFont:[UIFont fontWithName:@“BloggerSans”大小:14];
[self.followBtn从SuperView中移除];
如果(!self.statusObject.isFollowing){
[self.followBtn从SuperView中移除];
UIButton*followBtn=[[UIButton alloc]initWithFrame:CGRectMake(235,16,75,25)];
[followBtn setImage:nil for状态:uicontrol状态正常];
followBtn.layer.cornerRadius=2;//此值根据您的需要而变化
[followBtn.titleLabel setFont:[UIFont fontWithName:@“BloggerSans”大小:14.0f];
followBtn.titleEdgeInsets=UIEdgeInsetsMake(3,0,0);
followBtn.clipsToBounds=是;
[followBtn setTitleColor:[self colorWithHexString:@“ff68a8”]用于状态:UIControlStateNormal];
下一层边界宽度=1.5f;
followBtn.layer.borderColor=[[self colorWithHexString:@“ff68a8”]CGColor];
[followBtn设置标题:@“Follow”用于状态:UIControlStateNormal];
[followBtn addTarget:self action:@selector(follow)for controlEvents:UIControlEventTouchUpInside];
self.followBtn=followBtn;
[self addSubview:self.followBtn];
}
//如果默认值==nill,则将图标重置为默认值
[self-bringsubview-tofront:self.DefaultImgBorder];
[self-bringsubview-tofront:self.DefaultImgBtn];
UIImage*noDefault=[UIImage ImageName:@“女性”mini\u no\u default\u icon.png];
[self.DefaultImgBtn setBackgroundImage:noDefault for state:UIControlStateNormal];
//尝试创建六边形裁剪的默认照片
UIImage*hex_img=[UIImage ImageName:@“big_hex_thumb.png”];
UIImage*tan=[UIImage ImageName:@“feed_grey.png”];
UIImage*finalBG=[自我遮罩:带遮罩的棕褐色:十六进制图像];
[self.DefaultImgBorder setImage:finalBG];
NSURL*thumb_url=[NSURL URLWithString:[NSString stringWithFormat:@“%@%@”,s3thumbURL,self.statusObject.thumb_img];
@自动释放池{
[\u sdmanager下载带url:thumb\u url
选项:0
进度:^(NSInteger receivedSize,NSInteger expectedSize){}已完成:^(UIImage*图像,NSError*错误,SDImageCacheType cacheType,布尔已完成){
如果(图像)
{
UIImage*拇指img=图像;
UIImage*hex_img=[UIImage ImageName:@“big_hex_thumb.png”];
UIImage*noDefault=[UIImage ImageName:@“女性”mini\u no\u default\u icon.png];
[self.DefaultImgBtn setBackgroundImage:noDefault for state:UIControlStateNormal];
UIImage*finalImg=[自掩码:带掩码的拇指img:hex_img];
[self.DefaultImgBtn setBackgroundImage:finalImg for state:UIControlStateNormal];
}
}];
}
[self.DefaultImgBtn removeTarget:nil
操作:空