2011-12-08 1 views
0

UITableView(iOS開発版)を作成する必要があります。これは、各表のセル項目の隣に色付きのブロックを表示します。UITableViewカラーブロック

itemColorの値を含むNSMutableArrayオブジェクトがあります。テーブルを作成すると、itemColorを含むセルの隣にカラーブロックが表示されますか?

基本的に、Twitterにアバターが表示される方法と同じように、カスタムが必要ですか?UITableView

答えて

2

私のプロジェクトでも同様の要件がありました。 これは間違いなくあなたを助けるでしょう。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *cellIdentifier; 
    UITableViewCell *cell; 
    cellIdentifier = [NSString stringWithFormat:@"Cell Identifier"]; 

    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell==nil) { 
     cell = [self tableViewCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 
    } 
    [self configureCell:cell forIndexPath:indexPath]; 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    return cell; 
} 


-(UITableViewCell *)tableViewCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath{ 
    CGRect rect; 
    UILabel *label; 
    UIView *backView; 
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; 

    float center = (float) (CELL_HEIGHT-MAINFONT_SIZE)/2; 

    rect = CGRectMake(15, center, 150, MAINFONT_SIZE); 
    label = [[UILabel alloc] initWithFrame:rect]; 
    label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth; 
    label.tag = NAME_ID_TAG; 
    label.font = [UIFont boldSystemFontOfSize:MAINFONT_SIZE]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.textColor = [UIColor blackColor]; 
    //label.highlightedTextColor = [UIColor whiteColor]; 
    [cell.contentView addSubview:label]; 
    [label release]; 

    rect = CGRectMake(160, 10, 150, CELL_HEIGHT-20); 
    backView = [[UIView alloc] initWithFrame:rect]; 
    backView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |  UIViewAutoresizingFlexibleWidth; 
    backView.backgroundColor = [UIColor clearColor]; 
    backView.tag = BACK_VIEW_TAG; 



    [cell.contentView addSubview:backView]; 
    [backView release]; 

    return cell; 
} 



-(void)configureCell:(UITableViewCell *)tableViewCell forIndexPath:(NSIndexPath *)indexPath{ 
    //DeviceInfo *device; 
    UILabel *label; 
    PatientInfo *patInfo; 
    UIView *backView; 
    patInfo = [patientarray objectAtIndex:indexPath.row]; 

    label = (UILabel *)[tableViewCell viewWithTag:NAME_ID_TAG]; 
    label.text = patInfo.patientName; 

    backView = (UIView *)[tableViewCell viewWithTag:BACK_VIEW_TAG]; 


     backView.backgroundColor = UIColorFromRGB(patInfo.itemColor); 


} 
+0

私が言うことから、それは完璧です!私はまだココアや食べ物に少し新しいので、私は見ることができるこれの少しの例がありますか?ちょうど正しいものを手に入れることは、私にとっては苦労です。 – James

+1

このチュートリアルを見ると、テーブルビュー上にデータを表示する基本的な考え方が得られます。上記のコードを探します。 http://www.iosdevnotes.com/2011/10/uitableview-tutorial/ – virata

+0

パーフェクト!どうもありがとうございました:) – James