Objective c 在表格视图下方绘制一条垂直线

Objective c 在表格视图下方绘制一条垂直线,objective-c,ios,uitableview,Objective C,Ios,Uitableview,我想在SectionIndex列和单元格内容视图之间画一条垂直线。我该怎么做 请参阅本帖: 参考上面帖子里的克里斯·马克尔。它表明了这一联系: 注意:我从整个链接复制了代码并粘贴在这里,只是为了确保即使将来链接出现故障,答案仍然有用: UITableView可能是iPhone上使用最多的视图。它是 灵活的用户界面非常适合在iPhone上使用。有 关于如何向UITableViewCell添加多个项的大量示例。 然而,我需要以更传统的方式呈现一些数据 电子表格样式的网格。结果很好,使我能够打包 屏幕

我想在SectionIndex列和单元格内容视图之间画一条垂直线。我该怎么做

请参阅本帖:

参考上面帖子里的克里斯·马克尔。它表明了这一联系:

注意:我从整个链接复制了代码并粘贴在这里,只是为了确保即使将来链接出现故障,答案仍然有用:

UITableView可能是iPhone上使用最多的视图。它是 灵活的用户界面非常适合在iPhone上使用。有 关于如何向UITableViewCell添加多个项的大量示例。 然而,我需要以更传统的方式呈现一些数据 电子表格样式的网格。结果很好,使我能够打包 屏幕上有很多很难理解的信息 没有垂直网格。我将在这里展示一个非常简化的版本 可以使用将垂直线添加到UITableView

首先,我们需要创建UITableViewCell的子类。这就是我们 可以覆盖drawrect并绘制线条,并添加一个数组来保存 我们将画线的位置列表

在这个简化的示例中,我们将保留实际 在UITableViewController的单元格中输入文本并手动放置 完整的源代码附在最后。我们只是提供一个 绘制垂直线以形成网格的机制。列位置 通过调用addColumn来添加:

现在让我们覆盖drawRect。在其中,我们获取当前的图形上下文 并设置线条的颜色和宽度。然后我们迭代我们的列 从单元格行顶部到底部绘制直线的数组 数组中存储的每个位置

希望这有帮助。

请参阅以下帖子:

参考上面帖子里的克里斯·马克尔。它表明了这一联系:

注意:我从整个链接复制了代码并粘贴在这里,只是为了确保即使将来链接出现故障,答案仍然有用:

UITableView可能是iPhone上使用最多的视图。它是 灵活的用户界面非常适合在iPhone上使用。有 关于如何向UITableViewCell添加多个项的大量示例。 然而,我需要以更传统的方式呈现一些数据 电子表格样式的网格。结果很好,使我能够打包 屏幕上有很多很难理解的信息 没有垂直网格。我将在这里展示一个非常简化的版本 可以使用将垂直线添加到UITableView

首先,我们需要创建UITableViewCell的子类。这就是我们 可以覆盖drawrect并绘制线条,并添加一个数组来保存 我们将画线的位置列表

在这个简化的示例中,我们将保留实际 在UITableViewController的单元格中输入文本并手动放置 完整的源代码附在最后。我们只是提供一个 绘制垂直线以形成网格的机制。列位置 通过调用addColumn来添加:

现在让我们覆盖drawRect。在其中,我们获取当前的图形上下文 并设置线条的颜色和宽度。然后我们迭代我们的列 从单元格行顶部到底部绘制直线的数组 数组中存储的每个位置


希望这能有所帮助。

我在cellForRowAtIndexPath中使用了以下代码,这非常有效

static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];


if(cell == nil)
 {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:12.0f];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}



 [cell.contentView  setBounds:CGRectMake(cell.bounds.origin.x, cell.bounds.origin.y, 222,60)];

NSString *year = [self.keys objectAtIndex:[indexPath section]];
NSArray *movieSection = [[self.dataAllArtists objectForKey:year] valueForKey:@"row"];
NSDictionary *performanceDic = [movieSection objectAtIndex:[indexPath row]];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

 cell.textLabel.text = [NetworkData parseString:[performanceDic valueForKey:@"performance_name"]];


    int x;
    switch ([genreId intValue]) 
    {
     case 1:
      x=cell.contentView.bounds.size.width+19;
      break;
     case 3:
      x=cell.contentView.bounds.size.width+26;
      break;
      case 6:
      x=cell.contentView.bounds.size.width+16;
      break;
      case 7:
      x=cell.contentView.bounds.size.width+28;
      break;
     default:
      break;
    }

        for(UIView *view in [tableView subviews])
        {
         if([[[view class] description] isEqualToString:@"UITableViewIndex"])
         {
          [view setBackgroundColor:[UIColor whiteColor]];
         }
        }

  [cell.textLabel setLineBreakMode:UILineBreakModeWordWrap];
  [cell.textLabel setNumberOfLines:0];

  UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake(x,0,1, cell.contentView.bounds.size.height+2)] autorelease];


  lineView.backgroundColor = [UIColor lightGrayColor];
  [cell.contentView addSubview:lineView];


return cell;

我在cellForRowAtIndexPath中使用了下面的代码,这非常有效

static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];


if(cell == nil)
 {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:12.0f];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}



 [cell.contentView  setBounds:CGRectMake(cell.bounds.origin.x, cell.bounds.origin.y, 222,60)];

NSString *year = [self.keys objectAtIndex:[indexPath section]];
NSArray *movieSection = [[self.dataAllArtists objectForKey:year] valueForKey:@"row"];
NSDictionary *performanceDic = [movieSection objectAtIndex:[indexPath row]];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

 cell.textLabel.text = [NetworkData parseString:[performanceDic valueForKey:@"performance_name"]];


    int x;
    switch ([genreId intValue]) 
    {
     case 1:
      x=cell.contentView.bounds.size.width+19;
      break;
     case 3:
      x=cell.contentView.bounds.size.width+26;
      break;
      case 6:
      x=cell.contentView.bounds.size.width+16;
      break;
      case 7:
      x=cell.contentView.bounds.size.width+28;
      break;
     default:
      break;
    }

        for(UIView *view in [tableView subviews])
        {
         if([[[view class] description] isEqualToString:@"UITableViewIndex"])
         {
          [view setBackgroundColor:[UIColor whiteColor]];
         }
        }

  [cell.textLabel setLineBreakMode:UILineBreakModeWordWrap];
  [cell.textLabel setNumberOfLines:0];

  UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake(x,0,1, cell.contentView.bounds.size.height+2)] autorelease];


  lineView.backgroundColor = [UIColor lightGrayColor];
  [cell.contentView addSubview:lineView];


return cell;

@Princekumar:参考我的答案,看看它是否对您有帮助。@Princekumar:我的答案在不滚动表视图的情况下工作。它来自第一次显示表的时间。如果答案对您有帮助,请接受它。@Pricekumar:您需要将UITableViewCell子类化,然后覆盖其中的drawRect方法。此外,我还编辑了我的答案,并用粗体线条向您解释了答案。如果您需要更多帮助或有任何疑问,请让我知道。我应用了此答案,它再次正常工作,谢谢。Parth Bhatt先生,没有问题,它正常工作,但它在单元格中创建了两列,而不是我上面显示的单元格contentView和SectionIndex视图之间的一行。请查看在我的回答中,这将确认你我想要什么。@Princekumar:参考我的回答,看看它是否对你有帮助。@Princekumar:我的回答不需要滚动表格视图。它来自第一次显示表的时间。如果答案对您有帮助,请接受它。@Pricekumar:您需要将UITableViewCell子类化,然后覆盖其中的drawRect方法。此外,我还编辑了我的答案,并用粗体线条向您解释了答案。如果您需要更多帮助或有任何疑问,请让我知道。我应用了此答案,它再次正常工作,谢谢。Parth Bhatt先生,没有问题,它正常工作,但它在单元格n中创建了两列 单元格内容视图和部分索引视图之间有一条线,如我上面所示。请查看我的答案,这将确认你我想要什么。
- (void)drawRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // Use the same color and width as the default cell separator for now
    CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0);
    CGContextSetLineWidth(ctx, 0.25);

    for (int i = 0; i < [columns count]; i++) {
        CGFloat f = [((NSNumber*) [columns objectAtIndex:i]) floatValue];
        CGContextMoveToPoint(ctx, f, 0);
        CGContextAddLineToPoint(ctx, f, self.bounds.size.height);
    }

    CGContextStrokePath(ctx);

    [super drawRect:rect];
}
To add columns to the view just call

[cell addColumn:50];
when you’re building each cell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];

    MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil) {
        cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];

        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 30.0,
                                                           tableView.rowHeight)] autorelease];
        [cell addColumn:50];
        label.tag = LABEL_TAG;
        label.font = [UIFont systemFontOfSize:12.0];
        label.text = [NSString stringWithFormat:@"%d", indexPath.row];
        label.textAlignment = UITextAlignmentRight;
        label.textColor = [UIColor blueColor];
        label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
        UIViewAutoresizingFlexibleHeight;
        [cell.contentView addSubview:label]; 

        label =  [[[UILabel alloc] initWithFrame:CGRectMake(60.0, 0, 30.0,
                                                            tableView.rowHeight)] autorelease];
        [cell addColumn:120];
        label.tag = VALUE_TAG;
        label.font = [UIFont systemFontOfSize:12.0];
        // add some silly value
        label.text = [NSString stringWithFormat:@"%d", indexPath.row * 4];
        label.textAlignment = UITextAlignmentRight;
        label.textColor = [UIColor blueColor];
        label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
        UIViewAutoresizingFlexibleHeight;
        [cell.contentView addSubview:label];
    }

    return cell;
}
static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];


if(cell == nil)
 {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:12.0f];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}



 [cell.contentView  setBounds:CGRectMake(cell.bounds.origin.x, cell.bounds.origin.y, 222,60)];

NSString *year = [self.keys objectAtIndex:[indexPath section]];
NSArray *movieSection = [[self.dataAllArtists objectForKey:year] valueForKey:@"row"];
NSDictionary *performanceDic = [movieSection objectAtIndex:[indexPath row]];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

 cell.textLabel.text = [NetworkData parseString:[performanceDic valueForKey:@"performance_name"]];


    int x;
    switch ([genreId intValue]) 
    {
     case 1:
      x=cell.contentView.bounds.size.width+19;
      break;
     case 3:
      x=cell.contentView.bounds.size.width+26;
      break;
      case 6:
      x=cell.contentView.bounds.size.width+16;
      break;
      case 7:
      x=cell.contentView.bounds.size.width+28;
      break;
     default:
      break;
    }

        for(UIView *view in [tableView subviews])
        {
         if([[[view class] description] isEqualToString:@"UITableViewIndex"])
         {
          [view setBackgroundColor:[UIColor whiteColor]];
         }
        }

  [cell.textLabel setLineBreakMode:UILineBreakModeWordWrap];
  [cell.textLabel setNumberOfLines:0];

  UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake(x,0,1, cell.contentView.bounds.size.height+2)] autorelease];


  lineView.backgroundColor = [UIColor lightGrayColor];
  [cell.contentView addSubview:lineView];


return cell;