iOS Development--solving uicollectionview cell spacing and setting mismatch issues

Source: Internet
Author: User

When displaying data with Uicollectionview, we sometimes want to tune the cell spacing to a value that we want, and then check the API to see that there is a property:

-(CGFloat) minimuminteritemspacing {return
    0;
}

However, in many cases we will find that this writing does not meet our requirements, there is still a gap between the cell does not know how to produce.

We know that the spacing of the cell is determined by the size of the cell itemsize and the indented sectioninset of the section, through which Uicollectionview dynamically places the cell in the appropriate position, However, even if we continue to invoke the inset method it is no use.

-(uiedgeinsets) Sectioninset
{return
    uiedgeinsetsmake (0, 0, 0, 0);
}

< note: I want to achieve 0 spacing, other cases similar to >

As you can see, after inheriting uicollectionviewflowlayout, print out the results of these cell frame in the-Layoutattributesforelementsinrect: Method

-(nsarray*) Layoutattributesforelementsinrect: (cgrect) rect
{
    nsmutablearray* attributes = [[Super Layoutattributesforelementsinrect:rect] mutablecopy];

    
    For (uicollectionviewlayoutattributes *attr in attributes) {
        NSLog (@ "%@", Nsstringfromcgrect ([attr frame]);
    }
}


As you can see from the above two lines, my height is 42.8, but the X-value spacing of the two cell is 46, which means there is about 3px of space.


In fact, as Minimuminteritemspacing's name is, this property sets the minimum spacing, so what we actually need is a "maximuminteritemspacing", which is the maximum spacing. To solve this problem, you need to do some calculations yourself.

is still inherited uicollectionviewflowlayout, and then add the following code in the-Layoutattributesforelementsinrect: Method:

From the second loop to the last for
    (int i = 1; i < [attributes Count]; ++i) {
        //Current attributes
        Uicollectionviewlayoutattributes *currentlayoutattributes = attributes[i];
        Previous attributes
        uicollectionviewlayoutattributes *prevlayoutattributes = attributes[i-1];
        We want to set the maximum spacing that can be changed according to
        nsinteger maximumspacing = 0;
        The rightmost nsinteger of the previous cell is
        origin = Cgrectgetmaxx (prevlayoutattributes.frame);
        If the current cell's rightmost plus the spacing we want plus the current cell width is still in the contentsize, we change the current cell's origin position
        //Without this judgment the consequence is that Uicollectionview displays only one row, The reason is that the x values of all the cell below are appended to the last element of the first line
        if (Origin + maximumspacing + CurrentLayoutAttributes.frame.size.width < Self.collectionViewContentSize.width) {
            CGRect frame = currentlayoutattributes.frame;
            Frame.origin.x = Origin + maximumspacing;
            Currentlayoutattributes.frame = frame;
        }
    }

The reason note has been explained in great detail. This will solve the problem of cell spacing. Run again, the effect is very good:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.