0

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

在此处输入图像描述

帮我解决这个问题?

谢谢你。

4

2 回答 2

4

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

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

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

截屏

于 2013-06-04T08:28:01.560 回答
0

如果你想完全如图显示,请忽略我的回答。您可以使用两个 uitableviews 显示它。首先你需要创建一个表头名称为category的 tableview 必须有两行,即通信和会议。如果用户选择通信,然后导航到第二个表视图中的第二个表视图,每个部分的标题名称是 Mon、tue、Wed、Thur。然后在每一行中显示任务。

于 2013-06-04T06:13:40.803 回答