iOS SDK中的多行列tableView [英] Multi Row-Column tableView in iOS SDK

查看:31
本文介绍了iOS SDK中的多行列tableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,如何按照下图在 tableView 中显示数据.

In my app, How can I show the data in tableView as per following image.

帮我解决这个问题?

谢谢.

推荐答案

你可以使用 collectionView 来达到这个目的.因为它看起来像一个网格视图,所以 UICollectionView 将是使用的完美选择作为网格表.

You can use collectionView for this purpose.As it is looking like a grid View , so UICollectionView will be perfect choice to be used as grid sheet.

您可以使用 RFQuiltLayout 库,它是 UICollectionView.以下是可用于创建布局的代码:

You can use the RFQuiltLayout library, which is a subclass of UICollectionView. Here is the code you can use to create your layout:

- (void)viewDidLoad {

    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];

    RFQuiltLayout* layout = (id)[self.collectionView collectionViewLayout];
    layout.direction = UICollectionViewScrollDirectionVertical;
    layout.blockPixels = CGSizeMake(100, 40);
    [self.collectionView reloadData];
    [super viewDidLoad];
}

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

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier=@"cellIdentifier";

    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];


    UILabel* label = (id)[cell viewWithTag:21];
    if(!label) label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];

    label.backgroundColor=[UIColor blueColor];
    label.tag=21;
    label.textColor=[UIColor whiteColor];
    label.textAlignment=NSTextAlignmentCenter;
    [label setAutoresizingMask:(UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth)];
    [cell.contentView addSubview:label];

    label.text=[NSString stringWithFormat:@"Task%i",indexPath.row];
    if (indexPath.row==0) {
        label.text=@"Category";
    }

    if (indexPath.row==3) {
        label.text=@"Communication";
    }

    if (indexPath.row==8) {
        label.text=@"Meeting";
    }

    if (indexPath.row==13) {
        label.text=@"Others";
    }

    [cell setAutoresizesSubviews:YES];
    cell.backgroundColor=[UIColor greenColor];
    return cell;
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    UIEdgeInsets insets=UIEdgeInsetsMake(10, 10, 10, 10);
    return insets;
}

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

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

#pragma mark – RFQuiltLayoutDelegate

- (CGSize) blockSizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row==0)return CGSizeMake(2, 1);
    if (indexPath.row==3) return CGSizeMake(2, 2);
    if (indexPath.row==8) return CGSizeMake(2, 2);
    if (indexPath.row==13) return CGSizeMake(2, 2);

    return CGSizeMake(1, 1);
}

- (UIEdgeInsets)insetsForItemAtIndexPath:(NSIndexPath *)indexPath {
    return UIEdgeInsetsMake(2, 2, 2, 2);
}

这篇关于iOS SDK中的多行列tableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆