如何在swift中一行创建多列UITableViewCell?

时间:2015-05-11 11:45:58

标签: ios iphone uitableview swift

如何在一行中创建多个列。 喜欢这个图像! https://pbs.twimg.com/media/CEuO0hUUUAARX_L.png

1 个答案:

答案 0 :(得分:0)

您还可以使用gridview满足您的要求,例如GMGridViewUIGridView

检查以下示例代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellIdentifier = @"gridCell";
    UITableViewCell *gridCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(gridCell == nil)
    {
        gridCell =  [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier] autorelease];
        gridCell.accessoryType = UITableViewCellAccessoryNone;
        gridCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    NSArray* items = [_grids objectAtIndex:indexPath.section];

    int imageIndex = 0;
    int yOffset = 0;

    while (imageIndex < items.count)
    {
        int yPos = 4 + yOffset * 74;

        for(int i = 0; i < 4; ++i)
        {
            CGRect  rect = CGRectMake((18 + i * 80), yPos, 40, 40);

            UIImageView* imageView = [self gridImageRect:rect forImage:[items objectAtIndex:imageIndex]];
            [gridCell.contentView addSubview:imageView];
            [imageView release];

            rect = CGRectMake(((80 * i)-4), (yPos+44), 80, 12);

            UILabel* label = [self descriptionOfImage:imageIndex inRect:rect];
            [gridCell.contentView addSubview:label];
            [label release];

            ++imageIndex;

        }

        ++yOffset;
    }

    return gridCell;
}