objective-c - 在表格 View 下画一条垂直线

标签 objective-c ios uitableview

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

Split table view with table data and sorted keys

最佳答案

引用这篇文章:

how to add vertical lines to a table view in iphone sdk?

引用 克里斯·马克尔的 在上面的帖子中有。它建议这个链接:

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

注:我已从整个链接复制代码并将其粘贴到此处,以确保即使将来链接断开,答案仍然有用:

UITableView is probably the most used view on the iPhone. It’s flexible and the UI is ideally suited to use on the iPhone. There are lots of examples on how to add multiple items to a UITableViewCell. However, I needed to present some data in a more traditional spreadsheet style grid. The results worked well and enabled me to pack a lot of information on the screen that was very hard to follow without the vertical grid. I’ll show a very simplified version here you can use to add vertical lines to your UITableView.

First we need to create a subclass of UITableViewCell. This is so we can override drawrect and draw our lines and to add an array to hold a list of positions where we’ll draw the lines.


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

In this simplified example we’ll leave the positioning of the actual text in the cells in the UITableViewController and place it manually (full source code is attached at the end). We’re just providing a mechanism for drawing vertical lines to make a grid. Column locations are added by calling addColumn:


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

Now lets override drawRect. In it we grab the current graphics context and set the line color and width. Then we iterate over our columns array drawing a line from the top of the cell row to the bottom at each position stored in the array.


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

希望这可以帮助。

关于objective-c - 在表格 View 下画一条垂直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10070057/

相关文章:

ios - 如何在 iOS 中将照片保存到 google drive 的文件夹中

ios - 界面生成器中 UIView 的边框颜色不起作用?

iphone - 什么是 __NSArrayI 和 __NSArrayM?如何转换为 NSArray?

objective-c - 如果我强行将分配对象的指针指向 nil,ARC 会做什么?

ios - iOS 无法隐藏键盘

ios - 使用 UISearchDisplayController 作为父类(super class)

objective-c - 将 uint8_t 数组传递给方法

ios - didselectrowat Indexpath 使用两个发送者和一个公共(public) Segue 标识符

ios - 如何使用用户输入将行添加到 TableView ?

ios - 如何更新 TableView 中的数据(Swift)