1

SectionIndex 列とセル コンテンツ ビューの間に垂直線を引きたいと思います。これどうやってするの?

テーブル データと並べ替えられたキーを使用してテーブル ビューを分割する

4

2 に答える 2

3

この投稿を参照してください:

iphone SDKのテーブルビューに垂直線を追加する方法は?

上記の投稿にあるKrisMarkelを参照してください。それはこのリンクを示唆しています:

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

注:リンク全体からコードをコピーしてここに貼り付けました。これは、リンクが将来ダウンした場合でも回答が役立つようにするためです。

UITableViewは、おそらくiPhoneで最もよく使用されるビューです。柔軟性があり、UIは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 に答える