【问题标题】:is there a way to show Uitableviewcell selection on limited frame?有没有办法在有限的框架上显示 Uitableviewcell 选择?
【发布时间】:2010-01-07 15:27:18
【问题描述】:

我有一个 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;
}

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

【问题讨论】:

    标签: objective-c iphone-sdk-3.0 uitableview


    【解决方案1】:

    您可以在UITableViewCell 中创建一个自定义selectedBackgroundView,它具有您自己的框架和颜色。这将在选择单元格而不是默认的蓝色背景时显示。

    例如:

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 30, 40)];
    view.backgroundColor = [UIColor redColor];
    cell.selectedBackgroundView = view;
    [view release];
    

    【讨论】:

    • 但它显示在完整的单元格上。我用过这个。我只想在有限的框架上显示选择但仍然没有成功
    • 是的,我同意你不能设置自己的框架。反正对我没用。
    【解决方案2】:

    我今天遇到同样的问题,我通过这段代码解决了

    首先,在我的自定义单元格初始化中

         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];
    

    第二组 selectionStyle 无

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

    【讨论】:

      猜你喜欢
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 2021-11-16
      • 1970-01-01
      相关资源
      最近更新 更多