2009-09-11 92 views
3

默認情況下,iphone中有三種選擇樣式 - 表格視圖。iphone中的單元格選擇樣式

grey - blue - none。

我不需要灰色或藍色。

我想設置我的自定義。

例如在正常情況下,一個單元格應該有「aaaa.png」背景。

&選定的單元應具有「bbbbb.png」背景。

我試過應用cell.backgroundView & cell.selectedBackground視圖。但它不起作用。

如果您對此有任何想法,請幫助我。

在此先感謝您的幫助。

回答

8

這裏有三行代碼,但你不需要圖像!

UIView *selectedBackgroundViewForCell = [UIView new]; 
[selectedBackgroundViewForCell setBackgroundColor:[UIColor redColor]]; 
theCell.selectedBackgroundView = selectedBackgroundViewForCell; 
+0

工作正常...一個問題與它..當我使用分組表,它的第一行變成圓形,但選擇它不會變成圓形 – 2012-05-04 12:26:13

3

我試過以下&它的工作。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
NSString *CellIdentifier = [NSString stringWithFormat:@"%i",indexPath.row]; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    XML_FitnessPrograms *t=[arrayPrograms objectAtIndex:indexPath.row]; 
    cell=((indexPath.row%2)==0) ? 
    [self getCellContentView:CellIdentifier program_name:t.program_name alterNate:NO indexPath:indexPath] : 
    [self getCellContentView:CellIdentifier program_name:t.program_name alterNate:YES indexPath:indexPath] ; 
    CGRect a=CGRectMake(8, 0, 300, 44); 
    UIImageView *aImg=[[UIImageView alloc] initWithFrame:a]; 
    UIImageView *bImg=[[UIImageView alloc] initWithFrame:a]; 
    aImg.image=[UIImage imageNamed:@"white-rect-tab.png"]; //arrow.png 
    bImg.image=[UIImage imageNamed:@"white-rect-tab-copy.png"]; 
    [aImg setContentMode:UIViewContentModeScaleToFill]; 
    [bImg setContentMode:UIViewContentModeScaleToFill]; 
    cell.backgroundView=aImg; 
    cell.selectedBackgroundView=bImg; 
    [aImg release]; 
    [bImg release]; 
} 
return cell; 
} 

-(UITableViewCell*)getCellContentView:(NSString*)cellIdentifier program_name:(NSString*)program_name alterNate:(BOOL)alterNate indexPath:(NSIndexPath*)indexPath 
{ 

    UITableViewCell *cell; UILabel *tmp; 
    CGRect label1Frame=CGRectMake(5, 0, 260, 44); 
    cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; 
    cell.frame=CGRectMake(8, 0, 280, 44); 
    cell.backgroundColor=[UIColor clearColor]; 
    tmp=[[UILabel alloc] initWithFrame:label1Frame]; 
    tmp.textColor=[UIColor colorWithRed:(9.0/255.0) green:(68.0/255) blue:(85.0/255) alpha:1.0]; 
    [tmp setFont:[UIFont fontWithName:@"Arial-BoldMT" size:15]]; 
     tmp.text=program_name;     
     [tmp setShadowColor:[UIColor lightGrayColor]]; 
    tmp.backgroundColor=[UIColor clearColor]; 
     [cell.contentView addSubview:tmp]; [tmp release]; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleGray]; 
    return cell; 
} 
6

這裏的只有1行代碼做這件事:

cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pink.png"]] autorelease]; 

顯然,你需要做的圖像。

相關問題