针对奇数和偶数索引的不同项目的自定义网格视图

时间:2012-06-17 07:32:10

标签: iphone objective-c xcode

我在代码中使用了this custom grid view,我想创建一个如下所示的网格视图: enter image description here

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

- (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)));
}

}

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

只需使用uitableview操作即可。 在那个cellforrowatindexpath

根据奇数和偶数

设置按钮图像和帧
 if(indexpath.orw%2==0)
    {
    //set your frame here;

    }
    else
    {
   // set your frame here;
    }

答案 2 :(得分: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)));
}

}