删除uicollectionview单元格之间的差距

时间:2015-07-06 08:26:09

标签: ios objective-c

我想创建一个新闻应用程序。我设计了我的故事板in this case,结果就像this image。如何删除细胞之间的差距? 这是我的代码。在此代码中,我有一个集合视图和两个名为firstrowsecondrow

的行
@interface cillectionview ()
{
    NSMutableArray *images;
    NSMutableArray *titles;
    NSMutableArray *items;
    NSMutableArray *newssurls;
    NSMutableArray *namesOftags;
    NSString *liveurl;
    AppDelegate *appDelegate;
    int imgindex ;
}
@end

@implementation cillectionview
@synthesize mids,collectionView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{

    [super viewDidLoad];

    self.navigationController.navigationBar.barTintColor = UIColorFromRGB(0x303030);

//    
//    collectionView.delegate = self;
//    collectionView.dataSource = self;
    [collectionView setShowsVerticalScrollIndicator:NO];

    UICollectionViewFlowLayout *collectionViewLayout = (UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout;

    collectionViewLayout.collectionView.delegate =self;
    collectionViewLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    collectionViewLayout.minimumInteritemSpacing = 0;
    collectionViewLayout.minimumLineSpacing = 0;

    UIBarButtonItem *liveItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"liveitem@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(showLive:)];
    UIBarButtonItem *tagItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"tagitem@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(tagScreen)];


    UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"hispan@2x.png"] forState:UIControlStateNormal];
    [button setFrame:CGRectMake(0, 0, 80, 51)];
    UIBarButtonItem *logoButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = logoButton;
    logoButton.enabled = NO;


    [liveItem setTintColor:[UIColor orangeColor]];
    [tagItem setTintColor:[UIColor orangeColor]];

    NSArray *actionButtonItems = @[liveItem, tagItem];
    self.navigationItem.rightBarButtonItems = actionButtonItems;



    items = [[NSMutableArray alloc]init];
    images = [[NSMutableArray alloc]init];
    titles = [[NSMutableArray alloc]init];
    mids = [[NSMutableArray alloc]init];
    newssurls = [[NSMutableArray alloc]init];
    imgindex = 0;

    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    liveurl = [appDelegate getlivelink];
    mids = [appDelegate mIds];
    titles = [appDelegate mTitles];

    for (int i =0 ; i<[mids count]; i++) {
        NSMutableString* aUrl;
        if(i%3 == 0){
         aUrl = [NSMutableString stringWithFormat:@"http://hispantv.com/services/news.asmx/ContentsByCategory?category=%d&count=%d&imagesize=xl",[mids[i] intValue],1];
        }else{
         aUrl = [NSMutableString stringWithFormat:@"http://hispantv.com/services/news.asmx/ContentsByCategory?category=%d&count=%d&imagesize=l",[mids[i] intValue],1];
        }
        contentHelper *helper =[[contentHelper alloc]initWithDelegate:self  mainQuery:aUrl];
        [items  addObject:[helper newss]];
    }

    for (int i =0 ; i<[mids count]; i++) {
        NSMutableString* Url;
        Url = [NSMutableString stringWithFormat:@"http://hispantv.com/services/news.asmx/ContentsByCategory?category=%d&count=%d&imagesize=l",[mids[i] intValue],20];
        [newssurls  addObject:Url];
    }


    for (NSArray *n in items) {
        [images addObject:[n[0] image]];
    }


}


////////////



////////////

- (void)tagScreen
{
    [appDelegate createtag];
    namesOftags =[[NSMutableArray alloc]init];
    NSMutableArray *names = [[NSMutableArray alloc]init];
    NSMutableArray *ids = [[NSMutableArray alloc]init];
    namesOftags = [appDelegate GetTagName];


    for (NSMutableDictionary *strdic in namesOftags) {
        [names addObject:[strdic objectForKey:@"name"]];
        [ids addObject:[NSString stringWithFormat:@"%d",[[strdic objectForKey:@"id"]intValue]]];
    }

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    TagTableViewController *navigationController = [storyboard instantiateViewControllerWithIdentifier:@"TagTableViewController"];
    //navigationController.titleText = self.titleText;
    navigationController.cellTitles = names;
    navigationController.cellIds = ids;
    [self presentViewController:navigationController animated:YES completion:nil];
    [appDelegate GetChild:@""];

}


-(void) getContent:(NSMutableArray *)array{
    self.newss = [[NSMutableArray alloc]init];
    self.newss = array;
    //

    for (News *n in self.newss) {
        //NSLog(n.title);
    }
    NSLog(@"getcount:   #%i",[_newss count]);
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 6;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    NSString *identifier;




    switch (indexPath.row ) {
        case 0:
            imgindex =0;
            break;
        case 1:
            imgindex =0;
            break;
        case 2:
            imgindex =1;
            break;
        case 3:
            imgindex =1;
            break;
        case 4:
            imgindex =2;
            break;
        case 5:
            imgindex =2;
            break;

        default:
            break;
    }

    if(indexPath.row %2 == 0 ){
     identifier = @"Cell";
        firstrow *cell = (firstrow*)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

        [cell.bigimg setImageWithURL:[NSURL URLWithString:images[indexPath.row + imgindex ]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        cell.bTitle.text = titles[indexPath.row + imgindex];
        cell.bigimg.tag = indexPath.row + imgindex ;


        UITapGestureRecognizer *rtapRecognizer = [[UITapGestureRecognizer alloc]
                                                  initWithTarget:self action:@selector(bigimagetouchesBegan:)];
        [rtapRecognizer setNumberOfTouchesRequired:1];

        [rtapRecognizer setDelegate:self];
        cell.bigimg.userInteractionEnabled = YES;
        [cell.bigimg addGestureRecognizer:rtapRecognizer];

        return cell;

    }else {

        identifier = @"smallCell";
        secondrow * cell = (secondrow *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        [cell.leftimg setImageWithURL:[NSURL URLWithString:images[indexPath.row + imgindex ]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        cell.lTitle.text = titles[indexPath.row + imgindex];
        cell.leftimg.tag = indexPath.row + imgindex ;
        imgindex ++;
        cell.rTitle.text = titles[indexPath.row + imgindex];
        [cell.rightimg setImageWithURL:[NSURL URLWithString:images[indexPath.row + imgindex ]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        cell.rightimg.tag = indexPath.row + imgindex ;
        imgindex ++;

        UITapGestureRecognizer *rtapRecognizer = [[UITapGestureRecognizer alloc]
                                                 initWithTarget:self action:@selector(rtouchesBegan:)];
        [rtapRecognizer setNumberOfTouchesRequired:1];

        [rtapRecognizer setDelegate:self];
        cell.rightimg.userInteractionEnabled = YES;
        [cell.rightimg addGestureRecognizer:rtapRecognizer];

        UITapGestureRecognizer *ltapRecognizer = [[UITapGestureRecognizer alloc]
                                                 initWithTarget:self action:@selector(ltouchesBegan:)];
        [ltapRecognizer setNumberOfTouchesRequired:1];
        [ltapRecognizer setDelegate:self];
         cell.leftimg.userInteractionEnabled = YES;
        [cell.leftimg addGestureRecognizer:ltapRecognizer];


        return cell;
    }
}

#pragma mark - UICollectionViewDelegate

-(void) rtouchesBegan:(UITapGestureRecognizer *)gr
{
    UIImageView *theTappedImageView = (UIImageView *)gr.view;
    NSInteger tag = theTappedImageView.tag;
    NSMutableArray * a = [[NSMutableArray alloc ]init];

    contentHelper *helper =[[contentHelper alloc]initWithDelegate:self  mainQuery:[newssurls objectAtIndex:tag]];
    a =[helper newss];


    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    PageContentViewController *navigationController = [storyboard instantiateViewControllerWithIdentifier:@"contentViewController"];
    navigationController.newss = a;
    navigationController.titleText = [titles objectAtIndex:tag];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:navigationController];
    [self presentViewController:navController animated:YES completion:nil];

}
-(void) ltouchesBegan:(UITapGestureRecognizer *)gr
{
    UIImageView *theTappedImageView = (UIImageView *)gr.view;
    NSInteger tag = theTappedImageView.tag;
    NSLog(@" %d",tag);
    NSMutableArray * a = [[NSMutableArray alloc ]init];

    contentHelper *helper =[[contentHelper alloc]initWithDelegate:self  mainQuery:[newssurls objectAtIndex:tag]];
    a =[helper newss];


    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    PageContentViewController *navigationController = [storyboard instantiateViewControllerWithIdentifier:@"contentViewController"];
    navigationController.newss = a;
    navigationController.titleText = [titles objectAtIndex:tag];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:navigationController];
    [self presentViewController:navController animated:YES completion:nil];
}
-(void) bigimagetouchesBegan:(UITapGestureRecognizer *)gr
{
    UIImageView *theTappedImageView = (UIImageView *)gr.view;
    NSInteger tag = theTappedImageView.tag;
    NSLog(@" %d",tag);
    NSMutableArray * a = [[NSMutableArray alloc ]init];

    contentHelper *helper =[[contentHelper alloc]initWithDelegate:self  mainQuery:[newssurls objectAtIndex:tag]];
    a =[helper newss];

    NSLog(@"number is #%i",[a count]);


    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    PageContentViewController *navigationController = [storyboard instantiateViewControllerWithIdentifier:@"contentViewController"];
    navigationController.newss = a;
    navigationController.titleText = [titles objectAtIndex:tag];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:navigationController];
    [self presentViewController:navController animated:YES completion:nil];
} 

1 个答案:

答案 0 :(得分:0)

您需要:1。覆盖标准流程布局。 2.添加类似的实现:

- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
    NSArray *answer = [super layoutAttributesForElementsInRect:rect];

    for(int i = 1; i < [answer count]; ++i) {
        UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i];
        UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1];
        NSInteger maximumSpacing = 4;
        NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
        if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {
            CGRect frame = currentLayoutAttributes.frame;
            frame.origin.x = origin + maximumSpacing;
            currentLayoutAttributes.frame = frame;
        }
    }
    return answer;
}

其中maximumSpacing可以设置为您喜欢的任何值。这个技巧保证了单元格之间的空间完全等于maximumSpacing。

希望这会对你有所帮助。

更新回答:

#pragma mark Collection view layout things
// Layout: Set cell size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"SETTING SIZE FOR ITEM AT INDEX %d", indexPath.row);
    CGSize mElementSize = CGSizeMake(104, 104);
    return mElementSize;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 2.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 2.0;
}

// Layout: Set Edges
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
   // return UIEdgeInsetsMake(0,8,0,8);  // top, left, bottom, right
    return UIEdgeInsetsMake(0,0,0,0);  // top, left, bottom, right
}

使用此功能。