Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 滚动时单元格元素放错位置-CollectionView_Ios_Objective C_Uicollectionview_Cell_Uicollectionviewcell - Fatal编程技术网

Ios 滚动时单元格元素放错位置-CollectionView

Ios 滚动时单元格元素放错位置-CollectionView,ios,objective-c,uicollectionview,cell,uicollectionviewcell,Ios,Objective C,Uicollectionview,Cell,Uicollectionviewcell,我有一个自定义的UICollectionView。它看起来不错,一切都准备就绪,但一旦我开始滚动,一切都搞砸了。单元格中的图像位置错误、大小错误等 我的自定义ViewController如下所示 @implementation E5BaseDashboardViewController - (void)viewDidLoad { [super viewDidLoad]; self.horizontalPadding = 10.0f; self.vertical

我有一个自定义的UICollectionView。它看起来不错,一切都准备就绪,但一旦我开始滚动,一切都搞砸了。单元格中的图像位置错误、大小错误等

我的自定义
ViewController
如下所示

    @implementation E5BaseDashboardViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.horizontalPadding = 10.0f;
    self.verticalPadding = 10.0f;

    NSManagedObjectContext *context = [[E5RestKitManager sharedInstance] threadSafeManagedObjectContext];
    [context performBlockAndWait:^{
        self.collectionViewData = [[E5MenuPointController sharedInstance] rootEntityMenuPointsWithDrawerPosition:E5DrawerPositionCenter andContext:context];
    }];

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.965f green:0.961f blue:0.953f alpha:1.00f];

    // view is not hidden under navigation bar
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;

    // we need to move collectionView by the size of navigation bar + padding
        self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, CGRectGetHeight(self.navigationController.navigationBar.frame) + 2 * self.verticalPadding, 0);
    }

    [self.collectionView registerClass:[E5DashboardItemCell class] forCellWithReuseIdentifier:cell_identifier];

    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;

    [self.view addSubview:self.collectionView];
}

#pragma mark -
#pragma mark E5BaseDashboardViewController Data Source

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
    return [self.collectionViewData count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    E5DashboardItemCell *cell = [cv dequeueReusableCellWithReuseIdentifier:cell_identifier forIndexPath:indexPath];
    E5MenuPoint *menuPoint = (E5MenuPoint *) [self.collectionViewData objectAtIndex:indexPath.row];

    [cell configureCellWithMenuPoint: menuPoint];

    if ([[E5MenuPointController sharedInstance] isCategoryMenuPoint: menuPoint])
    {
        [cell.imageView setHidden:YES];
        [cell.circleBorder setHidden: YES];
    }
    else
    {
        [cell.imageView setHidden: NO];
        [cell.circleBorder setHidden: NO];
    }

    return cell;
}

#pragma mark -
#pragma mark E5BaseDashboardViewController Delegate layout setup

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    E5MenuPoint *menuPoint = (E5MenuPoint *) [self.collectionViewData objectAtIndex:indexPath.row];

    if ([[E5MenuPointController sharedInstance] isCategoryMenuPoint: menuPoint])
    {
        //set header cell size
        CGSize cellFrame =  CGSizeMake(100, 100);
        cellFrame.height = (self.view.frame.size.width - 20) / 4;
        cellFrame.width = self.view.frame.size.width - 20;

        return cellFrame;
    }
    else
    {
        NSNumber *num = [NSNumber numberWithInt:56];
        [menuPoint setCounter_notification_total: num];

        //set tile cell size
        CGSize cellFrame =  CGSizeMake(100, 100);
        cellFrame.height = (self.view.frame.size.width/2 - 15) * 1.25f;
        cellFrame.width = self.view.frame.size.width/2 - 15;

        return cellFrame;
    }
}

#pragma mark -
#pragma mark E5BaseDashboardViewController Delegate insets, spacing

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    //set cell insets
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    //set horizontal cell spacing
    return self.horizontalPadding;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    //set vertical cell spacing
    return self.verticalPadding;
}

#pragma mark -
#pragma mark E5BaseDashboardViewController Delegate selection

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [[E5MenuPointController sharedInstance] setNotificationCounterTo:0 forMenuPointWithReference:[(E5MenuPoint *)[self.collectionViewData objectAtIndex:indexPath.row] ref]];

    [E5ActionHandler performActionOfType:[(E5MenuPoint *)[self.collectionViewData objectAtIndex:indexPath.row] action_type]
                                    name:[(E5MenuPoint *)[self.collectionViewData objectAtIndex:indexPath.row] name]
                               reference:[(E5MenuPoint *)[self.collectionViewData objectAtIndex:indexPath.row] ref]
                         targetReference:[(E5MenuPoint *)[self.collectionViewData objectAtIndex:indexPath.row] action_target_ref]
                                  andUrl:[(E5MenuPoint *)[self.collectionViewData objectAtIndex:indexPath.row] action_url]
                          withStoryboard:self.storyboard
                     andDrawerController:self.mm_drawerController
                              backToRoot:YES];

    // track the action
    if([[(E5MenuPoint *)[self.collectionViewData objectAtIndex:indexPath.row] action_ref] length] > 0)
    {
        [[E5TrackingController sharedInstance] trackActionWithReference:[(E5MenuPoint *)[self.collectionViewData objectAtIndex:indexPath.row] action_ref]];
    }
}

#pragma mark -
#pragma mark dealloc

- (void)dealloc
{
    _collectionView.delegate = nil;
    _collectionView.dataSource = nil;
}

@end
自定义的
DashboardItemCell
类如下所示:

#import "E5DashboardItemCell.h"
#import "E5MenuPoint.h"
#import "E5MenuPointController.h"

@implementation E5DashboardItemCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self)
    {
        // change to our custom selected background view
        self.backgroundColor = [UIColor whiteColor];
        self.layer.borderWidth = 1.0f;
        self.layer.borderColor = [UIColor colorWithRed:0.925f green:0.925f blue:0.925f alpha:1.00f].CGColor;

        _circleBorder = [CAShapeLayer layer];
        [_circleBorder setFrame: CGRectMake(30, 20, self.frame.size.width - 60, self.frame.size.width - 60)];
        [_circleBorder setPath:[[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, _circleBorder.frame.size.width, _circleBorder.frame.size.height)] CGPath]];
        [_circleBorder setStrokeColor:[[UIColor colorWithRed:0.910f green:0.910f blue:0.898f alpha:1.00f] CGColor]];
        [_circleBorder setFillColor:[[UIColor clearColor] CGColor]];
        [self.layer addSublayer:self.circleBorder];

        CGFloat radius = self.circleBorder.frame.size.width/2;
        CGFloat positionNotificationCenter = sqrt(radius * radius/ 2);

        _notificationCircle = [CAShapeLayer layer];
        [_notificationCircle setFrame:CGRectMake(self.circleBorder.frame.origin.x + radius + positionNotificationCenter - radius/4,
                                                self.circleBorder.frame.origin.y + radius - positionNotificationCenter - radius/4,
                                                radius/2, radius/2)];
        [_notificationCircle setPath:[[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, _notificationCircle.frame.size.width, _notificationCircle.frame.size.height)] CGPath]];
        [_notificationCircle setStrokeColor:[[UIColor whiteColor] CGColor]];
        [_notificationCircle setFillColor:[[UIColor colorWithRed:0.949f green:0.349f blue:0.196f alpha:1.00f] CGColor]];
        [self.layer addSublayer:self.notificationCircle];

        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.circleBorder.frame.origin.x + self.circleBorder.frame.size.width/2 - self.circleBorder.frame.size.width/5,
                                                                   self.circleBorder.frame.origin.y + self.circleBorder.frame.size.height/2 - self.circleBorder.frame.size.height/5,
                                                                   self.circleBorder.frame.size.width/2.5,
                                                                   self.circleBorder.frame.size.height/2.5)];
        [self.layer addSublayer: self.imageView.layer];

        _counter = [[UILabel alloc] initWithFrame:CGRectMake(_notificationCircle.frame.origin.x, _notificationCircle.frame.origin.y, _notificationCircle.frame.size.width, _notificationCircle.frame.size.height)];
        _counter.textAlignment = NSTextAlignmentCenter;
        [_counter setFont:[UIFont boldSystemFontOfSize:15]];
        _counter.textColor = [UIColor whiteColor];
        [self.layer addSublayer: self.counter.layer];

        _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        [_textLabel setFont:[UIFont systemFontOfSize:15]];
        _textLabel.textAlignment=NSTextAlignmentCenter;
        _textLabel.numberOfLines = 0;
        _textLabel.lineBreakMode = NSLineBreakByWordWrapping;
        _textLabel.textColor = [UIColor colorWithRed:0.392f green:0.392f blue:0.380f alpha:1.00f];
        [self.layer addSublayer: self.textLabel.layer];
    }
    return self;
}

- (void)configureCellWithMenuPoint:(E5MenuPoint *)menuPoint
{
    BOOL isCategory = [[E5MenuPointController sharedInstance] isCategoryMenuPoint:menuPoint];

    // -- text --
    self.textLabel.frame = isCategory ? CGRectMake(10, 5, self.frame.size.width - 20, self.frame.size.height - 10) : CGRectMake(10, self.circleBorder.frame.origin.y + self.circleBorder.frame.size.height + 10, self.frame.size.width - 20 , self.frame.size.height - self.circleBorder.frame.size.height - self.circleBorder.frame.origin.y - 20);
    self.textLabel.text = menuPoint.name;

    // -- image --
    if([menuPoint.image_small length] > 0 | [menuPoint.image_medium length] > 0 | [menuPoint.image_large length] > 0)
    {
        // show image
        if (self.imageView == nil) {
            self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(52.5f,42.5f,40,40)];
            [self addSubview:self.imageView];
        }

        // get the most appropriate image size for the display
        NSString *imageURLAsString = [NSString string];
        if([E5Utils isRetinaDisplay])
        {
            // retina display image priorities: medium, large, small
            if([menuPoint.image_medium length] > 0)
            {
                imageURLAsString = menuPoint.image_medium;
            }
            else if([menuPoint.image_large length] > 0)
            {
                imageURLAsString = menuPoint.image_large;
            }
            else if([menuPoint.image_small length] > 0)
            {
                imageURLAsString = menuPoint.image_small;
            }
        } else {
            // non-retina display image priorities: small, medium, large
            if([menuPoint.image_small length] > 0)
            {
                imageURLAsString = menuPoint.image_small;
            } else if([menuPoint.image_medium length] > 0)
            {
                imageURLAsString = menuPoint.image_medium;
            } else if([menuPoint.image_large length] > 0)
            {
                imageURLAsString = menuPoint.image_large;
            }
        }

        if([imageURLAsString length] > 0)
        {
            [self.imageView setImageWithURL:[NSURL URLWithString:imageURLAsString] placeholderImage:[UIImage imageNamed:@"drawer_item_icon"]];
        }
    }
    else
    {
        if(self.imageView != nil)
        {
            // hide imageView
            [self.imageView removeFromSuperview];
            self.imageView = nil;
        }
    }

    // -- counter --
    [self.counter setText:[NSString stringWithFormat:@"%@", menuPoint.counter_notification_total]];

    if (menuPoint.counter_notification_total.integerValue < 9)
    {
        [_counter setFont:[UIFont boldSystemFontOfSize:13]];
    }
    else if (menuPoint.counter_notification_total.integerValue > 9 && menuPoint.counter_notification_total.integerValue < 99)
    {
        [_counter setFont:[UIFont boldSystemFontOfSize:10]];
    }
    else if (menuPoint.counter_notification_total.integerValue > 99)
    {
        [_counter setFont:[UIFont boldSystemFontOfSize:7]];
    }

    BOOL showCounter = FEATURE_MENUITEM_COUNTERS && menuPoint.countWithPermanent > 0;

    if (!isCategory && showCounter) {
        [self.counter setHidden:NO];
        [self.notificationCircle setHidden: NO];
    } else {
        [self.counter setHidden:YES];
        [self.notificationCircle setHidden: YES];
    }
}

@end
#导入“E5DashboardItemCell.h”
#导入“E5MenuPoint.h”
#导入“E5MenuPointController.h”
@DashboardItemCell的实现
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
如果(自我)
{
//更改为自定义选定的背景视图
self.backgroundColor=[UIColor whiteColor];
self.layer.borderWidth=1.0f;
self.layer.borderColor=[UIColor colorWithRed:0.925f绿色:0.925f蓝色:0.925f alpha:1.00f].CGColor;
_circleBorder=[CAShapeLayer layer];
[_circlebordersetframe:CGRectMake(30,20,self.frame.size.width-60,self.frame.size.width-60)];
[[u circleBorder setPath:[[UIBezierPath-bezierpath-withovalinrect:CGRectMake(0,0,[u circleBorder.frame.size.width,[u circleBorder.frame.size.height)]CGPath];
[_circlebordersetstrokecolor:[[UIColor-color-withred:0.910f-green:0.910f-blue:0.898f-alpha:1.00f]CGColor];
[[u circleBorder setFillColor:[[UIColor clearColor]CGColor]];
[self.layer addSublayer:self.circleBorder];
CGFloat半径=self.circleBorder.frame.size.width/2;
CGFloat位置通知中心=sqrt(半径*半径/2);
_notificationCircle=[CAShapeLayer layer];
[\u notificationCircle setFrame:CGRectMake(self.CircleOrder.frame.origin.x+半径+位置NotificationCenter-半径/4,
自.圆圈顺序.框架.原点.y+半径-位置通知中心-半径/4,
半径/2,半径/2)];
[[u notificationCircle setPath:[[UIBezierPath-bezierpath-withovalinrect:CGRectMake(0,0,[u notificationCircle.frame.size.width,[u notificationCircle.frame.size.height)]CGPath];
[\u通知圈设置行程颜色:[[UIColor whiteColor]CGColor];
[_notificationcirclesetfillcolor:[[UIColor-color-withred:0.949f-green:0.349f-blue:0.196f-alpha:1.00f]CGColor];
[self.layer addSublayer:self.notificationCircle];
_imageView=[[UIImageView alloc]initWithFrame:CGRectMake(self.circleBorder.frame.origin.x+self.circleBorder.frame.size.width/2-self.circleBorder.frame.size.width/5,
self.circleBorder.frame.origin.y+self.circleBorder.frame.size.height/2-self.circleBorder.frame.size.height/5,
自循环顺序。框架。尺寸。宽度/2.5,
自循环顺序、框架、尺寸、高度/2.5)];
[self.layer addSublayer:self.imageView.layer];
_计数器=[[UILabel alloc]initWithFrame:CGRectMake(_notificationCircle.frame.origin.x,_notificationCircle.frame.origin.y,_notificationCircle.frame.size.width,_notificationCircle.frame.size.height)];
_counter.textAlignment=NSTextAlignmentCenter;
[_countersetfont:[UIFont-boldSystemFontOfSize:15];
_counter.textColor=[UIColor-whiteColor];
[self.layer addSublayer:self.counter.layer];
_textLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,0,0,0)];
[_textLabelsetFont:[UIFont systemFontOfSize:15];
_textLabel.textAlignment=NSTextAlignmentCenter;
_textLabel.numberOfLines=0;
_text label.lineBreakMode=NSLineBreakByWordWrapping;
_textLabel.textColor=[UIColor颜色与红色:0.392f绿色:0.392f蓝色:0.380fα:1.00f];
[self.layer addSublayer:self.textlab.layer];
}
回归自我;
}
-(无效)使用菜单点配置单元格:(E5菜单点*)菜单点
{
BOOL isCategory=[[E5MenuPointController sharedInstance]isCategoryMenuPoint:menuPoint];
//--文本--
self.textlab.frame=isCategory?CGRectMake(10,5,self.frame.size.width-20,self.frame.size.height-10):CGRectMake(10,self.circleBorder.frame.origin.y+self.circleBorder.frame.size.height+10,self.frame.size.width-20,self.circleBorder.frame.size.height-self.circleBorder.frame.y-20);
self.textlab.text=menuPoint.name;
//--形象--
如果([menuPoint.image_小长度]>0 |[menuPoint.image_中长度]>0 |[menuPoint.image_大长度]>0)
{
//显示图像
如果(self.imageView==nil){
self.imageView=[[UIImageView alloc]initWithFrame:CGRectMake(52.5f,42.5f,40,40)];
[self addSubview:self.imageView];
}
//为显示器获取最合适的图像大小
NSString*imageURLAsString=[NSString字符串];
如果([E5Utils isRetinaDisplay])
{
//视网膜显示图像优先级:中、大、小
如果([menuPoint.image_medium length]>0)
{
imageURLAsString=menuPoint.image\u介质;
}
如果([menuPoint.image_large length]>0,则为else)
{
imageURLAsString=menuPoint.image\u大;
}
如果([menuPoint.image_小长度]>0),则为else
{
imageURLAsString=menuPoint.image\u小;
}
}否则{
//非视网膜显示图像优先级:小、中、大
如果([menuPoint.image_小长度]>0)
{
imageURLAsString=menuPoint.image\u小;
}如果([menuPoint.image_medium length]>0,则为else)
{
imageURLAsString=menuPoint.image\u介质;
}如果([menuPoint.image_large length]>0,则为else)
{
imageURLAsString=menuPoint.image\u大;
}
}