简体   繁体   English

iPhone 中的单元格选择样式

[英]Cell selection style in iPhone

By default there is three selection style in iPhone - table view.默认情况下,iPhone 中有三种选择样式 - 表格视图。

gray - blue - none.灰色 - 蓝色 - 无。

I don't need gray or blue.我不需要灰色或蓝色。

I want to set my custom.我想设置我的自定义。

For example, in normal situation a cell should have "aaaa.png" background, and selected cell should have "bbbbb.png" background.例如,在正常情况下,一个单元格应该有“aaaa.png”背景,而选中的单元格应该有“bbbbb.png”背景。

I have tried to apply cell.backgroundView & cell.selectedBackground view.我试图应用 cell.backgroundView 和 cell.selectedBackground 视图。 However it isn't working.但是它不起作用。

Three lines of code here, but you don't need an image!这里三行代码,但你不需要图像!

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

Here's one way to do it with only 1 line of code:这是仅使用 1 行代码即可完成的一种方法:

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

obviously you'll need to make the image.显然你需要制作图像。

I tried following & it worked.我试过跟随&它奏效了。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM