2012-04-09 92 views

回答

3

參考這個帖子:

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

參考克里斯·馬克爾的沒有在上面的帖子。這表明此鏈接:

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

希望這有助於。

+0

@Princekumar:請參閱我的回答,看看它是否可以幫助你。 – 2012-04-09 07:42:23

+0

謝謝!它幫了我很多。 – Warewolf 2012-04-10 04:11:32

+0

@Princekumar:我的答案沒有滾動表視圖。它來自第一次顯示錶格。接受答案,如果它可以幫助你。 – 2012-04-11 04:23:52

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;