0

我在我的代码中使用了这个自定义网格视图,我想创建一个如下所示的网格视图: 在此处输入图像描述

我应该如何在代码中执行此操作?这是项目的设置方法:

- (void)setupItemViews {
for (UIView *view in self.itemViews) {
    [view removeFromSuperview];
}
for (UIImageView *image in self.images) {
    [image removeFromSuperview];
}

[self.images removeAllObjects];
[self.itemViews removeAllObjects];

// now add the new objects
NSUInteger numItems = [self.menuDelegate menuViewNumberOfItems:self];

for (NSUInteger i = 0; i < numItems; i++) {
    GridMenuItemView *itemView = [[GridMenuItemView alloc] init];

    GridMenuItem *menuItem = [self.menuDelegate menuView:self itemForIndex:i];

    itemView.frame = CGRectMake(0, 0, self.itemSize.width, self.itemSize.height);
    itemView.label.text = menuItem.title;
    //itemView.imageView.image = menuItem.icon;
    itemView.tag = i;

    [itemView addTarget:self
                 action:@selector(itemPressed:) 
       forControlEvents:UIControlEventTouchUpInside];

    [itemView setBackgroundImage:[UIImage imageNamed:menuItem.normalImageName]
                        forState:UIControlStateNormal];
    [itemView setBackgroundImage:[UIImage imageNamed:menuItem.highlightedImageName]
                        forState:UIControlStateHighlighted];

    NSUInteger numColumns = self.bounds.size.width > self.bounds.size.height ? self.columnCountLandscape : self.columnCountPortrait;


    [self.itemViews addObject:itemView];
    [self addSubview:itemView];


}

感谢ctrahey,我得到了我想要的。但是用于设置单元格的方法-(void) layoutSubviews如下:

在这行之后可以找到对应的代码:**//__n__ ... - (void)layoutSubviews { [super layoutSubviews]; //[self removeFromSuperview];

NSUInteger numColumns = self.bounds.size.width > self.bounds.size.height ? self.columnCountLandscape : self.columnCountPortrait;

NSUInteger numItems = [self.menuDelegate menuViewNumberOfItems:self];
if (self.itemViews.count != numItems) {
    [self setupItemViews];
}

CGFloat padding = roundf((self.bounds.size.width - (self.itemSize.width * numColumns)) / (numColumns + 1));
NSUInteger numRows = numItems % numColumns == 0 ? (numItems / numColumns) : (numItems / numColumns) + 1;

CGFloat yPadding = 0;
CGFloat totalHeight = ((self.itemSize.height + yPadding) * numRows) + yPadding ;

// get an even y padding if less than the max number of rows

if (totalHeight < self.bounds.size.height) {
    CGFloat leftoverHeight = self.bounds.size.height - totalHeight;
    CGFloat extraYPadding = roundf(leftoverHeight / (numRows + 1));
    yPadding += extraYPadding;

    totalHeight = ((self.itemSize.height + yPadding) * numRows) + yPadding;
}

// get an even x padding if we have less than a single row of items
if (numRows == 1 && numItems < numColumns) {
    padding = roundf((self.bounds.size.width - (numItems * self.itemSize.width)) / (numItems + 1));
}

for (int i = 0, j = numColumns; i < numItems; i++) {

    j--;
    if (j<0) {
        j += numColumns;
    }
    //NSUInteger column = j ;
    //NSUInteger row = i / numColumns;
    UIView *item = [self.itemViews objectAtIndex:i];


    //CGFloat xOffset = (column * (self.itemSize.width + padding)) + padding;
    //CGFloat yOffset = (row * (self.itemSize.height + yPadding));// + yPadding;


    **//__n__ These three lines of code make set the cell items at different xPositions from right and left.  
    CGFloat xPosition = (i%2) ? 0.0 : self.bounds.size.width - self.itemSize.width;
    CGFloat yPosition = i*self.itemSize.height;
    item.frame = CGRectMake(xPosition, yPosition, self.itemSize.width, self.itemSize.height);**

    //item.frame = CGRectMake(xOffset, yOffset, self.itemSize.width, self.itemSize.height);

  //  if ((i%numColumns) == 0) {
     //   UIImageView *img = [self.images objectAtIndex:i/numColumns];
     //   img.frame = CGRectMake(0, yOffset+76, self.bounds.size.width, 1);


 //   }//End If
}
    //Scroll size.
UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];


if (UIDeviceOrientationIsPortrait(currentOrientation))
{
    self.contentSize = CGSizeMake(self.bounds.size.width, totalHeight + (yPadding * (numRows+1.55)));
}


else if (UIDeviceOrientationIsLandscape(currentOrientation))
{

    self.contentSize = CGSizeMake(self.bounds.size.width, totalHeight + (yPadding * (numRows+3)));
}

}

4

3 回答 3

1

如果我正确理解您的意图,则该行:

itemView.frame = CGRectMake(0, 0, self.itemSize.width, self.itemSize.height);

应该使用您的索引 i 和一些简单的算术来定位视图。我过去使用过的一种技术是使用模运算符进行简单的奇数检查。

CGFloat xPosition = (i%2) ? 0.0 : self.bounds.size.width - self.itemSize.width;
CGFloat yPosition = i*self.itemSize.height;
itemView.frame = CGRectMake(xPosition, yPosition, self.itemSize.width, self.itemSize.height);
于 2012-06-17T07:47:31.170 回答
0

感谢 ctrahey。用于设置单元格的方法-(void) layoutSubviews如下:

在这行之后可以找到对应的代码:**//__n__ ... - (void)layoutSubviews { [super layoutSubviews]; //[self removeFromSuperview];

NSUInteger numColumns = self.bounds.size.width > self.bounds.size.height ? self.columnCountLandscape : self.columnCountPortrait;

NSUInteger numItems = [self.menuDelegate menuViewNumberOfItems:self];
if (self.itemViews.count != numItems) {
    [self setupItemViews];
}

CGFloat padding = roundf((self.bounds.size.width - (self.itemSize.width * numColumns)) / (numColumns + 1));
NSUInteger numRows = numItems % numColumns == 0 ? (numItems / numColumns) : (numItems / numColumns) + 1;

CGFloat yPadding = 0;
CGFloat totalHeight = ((self.itemSize.height + yPadding) * numRows) + yPadding ;

// get an even y padding if less than the max number of rows

if (totalHeight < self.bounds.size.height) {
    CGFloat leftoverHeight = self.bounds.size.height - totalHeight;
    CGFloat extraYPadding = roundf(leftoverHeight / (numRows + 1));
    yPadding += extraYPadding;

    totalHeight = ((self.itemSize.height + yPadding) * numRows) + yPadding;
}

// get an even x padding if we have less than a single row of items
if (numRows == 1 && numItems < numColumns) {
    padding = roundf((self.bounds.size.width - (numItems * self.itemSize.width)) / (numItems + 1));
}

for (int i = 0, j = numColumns; i < numItems; i++) {

    j--;
    if (j<0) {
        j += numColumns;
    }
    //NSUInteger column = j ;
    //NSUInteger row = i / numColumns;
    UIView *item = [self.itemViews objectAtIndex:i];


    //CGFloat xOffset = (column * (self.itemSize.width + padding)) + padding;
    //CGFloat yOffset = (row * (self.itemSize.height + yPadding));// + yPadding;


    **//__n__ These three lines of code make set the cell items at different xPositions from right and left.  
    CGFloat xPosition = (i%2) ? 0.0 : self.bounds.size.width - self.itemSize.width;
    CGFloat yPosition = i*self.itemSize.height;
    item.frame = CGRectMake(xPosition, yPosition, self.itemSize.width, self.itemSize.height);**

    //item.frame = CGRectMake(xOffset, yOffset, self.itemSize.width, self.itemSize.height);

  //  if ((i%numColumns) == 0) {
     //   UIImageView *img = [self.images objectAtIndex:i/numColumns];
     //   img.frame = CGRectMake(0, yOffset+76, self.bounds.size.width, 1);


 //   }//End If
}
    //Scroll size.
UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];


if (UIDeviceOrientationIsPortrait(currentOrientation))
{
    self.contentSize = CGSizeMake(self.bounds.size.width, totalHeight + (yPadding * (numRows+1.55)));
}


else if (UIDeviceOrientationIsLandscape(currentOrientation))
{

    self.contentSize = CGSizeMake(self.bounds.size.width, totalHeight + (yPadding * (numRows+3)));
}

}

于 2012-06-17T12:22:16.880 回答
0

只需使用 uitableview 操作。在那个牢房forrowatindexpath

根据奇偶设置按钮图像和框架

 if(indexpath.orw%2==0)
    {
    //set your frame here;

    }
    else
    {
   // set your frame here;
    }
于 2012-06-17T07:48:59.570 回答