代码之家  ›  专栏  ›  技术社区  ›  Rahul Vyas

是否有方法在有限帧上显示UITableViewCell选择?

  •  2
  • Rahul Vyas  · 技术社区  · 15 年前

    我有一个UITableView单元格添加了一些这样的标签

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
     static NSString *CellIdentifier = @"Cell"; 
    
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    
         UIImage* myImage = [UIImage imageNamed:@"AccessoryForDealsMain.png"];
         UIImageView *MyimageView = [[UIImageView alloc] initWithImage:myImage];
         MyimageView.contentMode=UIViewContentModeScaleToFill;
         cell.accessoryView=MyimageView;
         [MyimageView release];
    
         UILabel *BluePatti = [[UILabel alloc] init];
         BluePatti.frame=CGRectMake(0,0,4,44);
         BluePatti.backgroundColor = BLUEPATTI;//[UIColor blueColor];
         UILabel *BlackLine = [[UILabel alloc] init];
         BlackLine.frame=CGRectMake(3,0,1, 45);
         BlackLine.backgroundColor = BLACK_LINE;
         [BluePatti addSubview:BlackLine];
         [BlackLine release];
    
         [cell.contentView addSubview:BluePatti];
         [BluePatti release];
    
         UILabel *Seperator = [[UILabel alloc] initWithFrame:CGRectZero];
         Seperator.backgroundColor = [UIColor colorWithRed:234/256.0 green:234/256.0 blue:234/256.0 alpha:1.0]; //[UIColor lightGrayColor];
         Seperator.frame = CGRectMake(4,43,316,1);
         [cell.contentView addSubview:Seperator];
         [Seperator release];
    }
    if(indexPath.section==5)
    {
        cell.textLabel.text=[self.GenderCellData objectAtIndex:indexPath.row];
        cell.textLabel.textColor=CELL_TEXT_COLOR;
        cell.textLabel.font=CELL_FONT;
    }
    else
    {
        cell.textLabel.text=@"Cells";
        cell.textLabel.font=CELL_FONT;
    }
    return cell;
    }
    

    现在,当我触摸手机时,它会在所有子视图上方显示蓝色选择。如何更改选择框和颜色?

    2 回复  |  直到 15 年前
        1
  •  0
  •   Adam Woś    15 年前

    您可以创建自定义 selectedBackgroundView 在里面 UITableViewCell 它有你自己的框架和颜色。当选择单元格而不是默认的蓝色背景时,将显示此选项。

    例如:

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 30, 40)];
    view.backgroundColor = [UIColor redColor];
    cell.selectedBackgroundView = view;
    [view release];
    
        2
  •  0
  •   Stephen Chen    10 年前

    我今天也面临同样的问题,我通过这个代码解决了这个问题。

    首先,在我的自定义单元格init中

         self.customerbgImageview = [UIImageView new];
        [self.customerbgImageview setFrame:CGRectMake(0 , 11, Width, 66)];
        [self.customerbgImageview setBackgroundColor:UIColor_RGB(0, 255, 255, 0.5)];
        [self addSubview:self.customerbgImageview];
    

    第二组选择样式无

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    表中第三种方法

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        CustomOrderTableViewCell *cell = (CustomOrderTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
        [cell.customerbgImageview setHidden:NO];
    
    }
    
    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
        CustomOrderTableViewCell *cell = (CustomOrderTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
        [cell.customerbgImageview setHidden:YES];
    }
    
    推荐文章