【问题标题】:Create multi-column table using TableViews in iOS在 iOS 中使用 TableViews 创建多列表
【发布时间】:2015-12-09 13:07:36
【问题描述】:

我在iOS 中使用多个TableViews 创建多列表。如附图所示,即TableViews并排放置。由于TableView具备每个单元格的TapGesture、Scrolling等所有功能,我喜欢我的实现方式。

唯一的问题是滚动。由于每个 TableView 都有自己的滚动,因此我在彼此之间同步滚动时遇到了问题。否则,每行不同列的所有数据都将不匹配。

如何在 TableView 之间同步滚动,即如果我滚动一个 TableView,所有 TableView 应该一起滚动。

谢谢。

【问题讨论】:

  • IOS中使用UICollectionView创建的多列表
  • UICollectionView 更像是照片应用。我只需要一个像excel这样的表格。不需要图片。当然,我需要每个单元格的 TapGesture。
  • 然后为collectionview创建类似excel的布局。有什么问题?
  • 为什么你认为UICollectionView更像是照片应用?集合视图可以看起来像您想要的任何东西。只需使用适当的布局和设置。

标签: ios objective-c uitableview


【解决方案1】:

有一些方法可以让它工作。但这将是错误的,并且在我看来,解决问题的方法不正确。对于这个问题,我会使用UICollectionView。这样,您就有了一个dataSource,并且比UITableView 更灵活。您也不需要使用任何 hacks 来同步滚动。

【讨论】:

  • UICollectionView 更像是照片应用。我只需要一个像excel这样的表格。不需要图片。当然,我需要每个单元格的 TapGesture。
  • UICollectionView 能够显示的不仅仅是照片。 ;)
  • ic 让我测试 UICollectionView。
  • @batuman 请记住,UICollectionView 与 UITableView 几乎相同。它只是在类固醇上。可以随心所欲地设计和布局单元格。
【解决方案2】:

将你的 UITableView 分成你想显示的列数,就像我在示例代码中所做的那样,然后添加自定义标签并为它们提供标签

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


    if(cell==nil)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ci];
    }


    if (tableView == your tablename)
    {

    CGFloat widthLabelHeader = CGRectGetWidth(tablename.frame)/7; // 7 is the number of columns in table view

serialnmbrlbl = (UILabel*)[cell.contentView viewWithTag:401];

    if(!serialnmbrlbl)

    {

        serialnmbrlbl = [[UILabel alloc]init];
        serialnmbrlbl.frame = CGRectMake(0, 0, widthLabelHeader, 50);
        serialnmbrlbl.layer.borderWidth = 0.5;
        serialnmbrlbl.tag = 401;
        serialnmbrlbl.font = [UIFont fontWithName:@"ArialHebrew-Light" size:14];
        [cell.contentView addSubview:serialnmbrlbl];
    }
    serialnmbrlbl.textAlignment = NSTextAlignmentCenter;


    diagnosisnamelbl = (UILabel*)[cell.contentView viewWithTag:402];

    if(!diagnosisnamelbl)

    {

        diagnosisnamelbl = [[UILabel alloc]init];
        diagnosisnamelbl.frame = CGRectMake(0, 0, widthLabelHeader, 50);
        diagnosisnamelbl.layer.borderWidth = 0.5;
        diagnosisnamelbl.tag = 402;
        diagnosisnamelbl.font = [UIFont fontWithName:@"ArialHebrew-Light" size:14];
        [cell.contentView addSubview:diagnosisnamelbl];
    }
    diagnosisnamelbl.textAlignment = NSTextAlignmentCenter;


    dischargeamtlbl = (UILabel*)[cell.contentView viewWithTag:403];

    if(!dischargeamtlbl)

    {

        dischargeamtlbl = [[UILabel alloc]init];
        dischargeamtlbl.frame = CGRectMake(0, 0, widthLabelHeader, 50);
        dischargeamtlbl.layer.borderWidth = 0.5;
        dischargeamtlbl.tag = 403;
        dischargeamtlbl.font = [UIFont fontWithName:@"ArialHebrew-Light" size:14];
        [cell.contentView addSubview:dischargeamtlbl];
    }
    dischargeamtlbl.textAlignment = NSTextAlignmentCenter;

然后应用第二个委托方法

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{
    if(tableView == hospitaltable)
    {
        UIView *sectionHeaderView = [[UIView alloc] initWithFrame:
                                     CGRectMake(0, 0, hospitaltable.frame.size.width, 45.0)];
        sectionHeaderView.backgroundColor = [UIColor colorWithRed:(83/255.f) green:(200/255.f) blue:(36/255.f) alpha:1];

        CGFloat widthLabelHeader = CGRectGetWidth(hospitaltable.frame)/7;


        UILabel *namelabel1 = (UILabel*)[sectionHeaderView viewWithTag:201];
        if(!namelabel1)
        {

            namelabel1 = [[UILabel alloc]init];
            namelabel1.frame = CGRectMake(0, 0, widthLabelHeader, 45);
            namelabel1.layer.borderWidth = 0.5;
            namelabel1.font = [UIFont fontWithName:@"ArialHebrew-Light" size:14];
            [sectionHeaderView addSubview:namelabel1];
        }
        namelabel1.text = @"Sr. NO.";
        namelabel1.textAlignment = NSTextAlignmentCenter;
        namelabel1.lineBreakMode = NSLineBreakByTruncatingMiddle;
        namelabel1.numberOfLines = 2;

        namelabel1.backgroundColor = [UIColor colorWithRed:(83/255.f) green:(200/255.f) blue:(36/255.f) alpha:1];


        UILabel *orderdateLbl = (UILabel*)[sectionHeaderView viewWithTag:202];
        if (!orderdateLbl) {
            orderdateLbl = [[UILabel alloc]init];
            orderdateLbl.frame = CGRectMake(CGRectGetMaxX(namelabel1.frame), 0, widthLabelHeader, 45);
            orderdateLbl.layer.borderWidth = 0.5;
            orderdateLbl.font = [UIFont fontWithName:@"ArialHebrew-Light" size:14];
            [sectionHeaderView addSubview:orderdateLbl];
        }
        orderdateLbl.text = @"DIAGNOSIS NAME";
        orderdateLbl.textAlignment = NSTextAlignmentCenter;
        orderdateLbl.lineBreakMode = NSLineBreakByTruncatingMiddle;
        orderdateLbl.numberOfLines = 2;

        orderdateLbl.backgroundColor = [UIColor colorWithRed:(83/255.f) green:(200/255.f) blue:(36/255.f) alpha:1];

也应用这个委托方法..

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

        return 45;
}

最后

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{
  return 45;
}

借助此代码,您将获得所需的 tableView

【讨论】:

  • 如果用户想要处理不同列的触摸,这会有所帮助吗?
  • 在标签上提供点击手势
  • @Abhi,在滚动中,你如何保持数据在同一行?因为每个 TableView 都有自己的滚动条。
  • 这实际上并没有试图解决问题。您不能将单元格彼此相邻放置,并且由于您的解决方案使用一个 UITableView 它根本无法解决@batuman 的问题。 UICollectionView 是这样做的正确方法。
猜你喜欢
  • 1970-01-01
  • 2020-02-25
  • 2022-09-23
  • 2018-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多