1

我想在 SectionIndex 列和单元格内容视图之间画一条垂直线。我怎样才能做到这一点?

使用表数据和排序键拆分表视图

4

2 回答 2

3

参考这篇文章:

如何在 iphone sdk 的表格视图中添加垂直线?

请参阅上面帖子中的Kris Markel 。它建议这个链接:

http://www.iphonedevx.com/?p=153

注意:我已经从整个链接中复制了代码并将其粘贴到此处,以确保即使链接将来出现故障,答案仍然有用:

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

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

@interface MyTableCell : UITableViewCell {
    NSMutableArray *columns;
}
- (void)addColumn:(CGFloat)position;
@end

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

- (void)addColumn:(CGFloat)position {
    [columns addObject:[NSNumber numberWithFloat:position]];
}

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

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

希望这可以帮助。

于 2012-04-09T07:24:42.740 回答
2

我在 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;
于 2012-04-10T04:08:56.963 回答