【问题标题】:how to change the background image of the cell in didSelectRowAtIndexPath如何更改 didSelectRowAtIndexPath 中单元格的背景图像
【发布时间】:2014-01-23 19:20:18
【问题描述】:

我想在选择单元格时更改单元格的背景图像。

我已经尝试过这种语法:

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
 //for adding image
 cell.ImgCateg.image=[UIImage imageNamed:[ImgArr objectAtIndex:indexPath.row]];
     ...
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CRHomeCategCell *cell = (CRHomeCategCell*)[_tblCateg cellForRowAtIndexPath:indexPath];
    cell.ImgCateg.image = [UIImage imageNamed:@"DefaultBg.png"];
}

这会改变前景中的图像而不是背景。

请提前帮助和感谢。

【问题讨论】:

    标签: ios iphone ipad uitableview uiimageview


    【解决方案1】:

    您可以在cellForRowAtIndexPath方法中设置UITableviewCell的属性setSelectedBackgroundView

    喜欢,

    - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
    {
    ....
    UIImageView *normalCellBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
    
    [normalCellBG setImage:[UIImage imageNamed:@"box_nonselected.png"]];//Set Image for Normal
    [normalCellBG setBackgroundColor:[UIColor clearColor]];
    [cell setBackgroundView:normalCellBG];
    
    
    UIImageView *selecetedCellBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
    [selecetedCellBG setImage:[UIImage imageNamed:@"box_selected.png"]];//Set Name for selected Cell
    [selecetedCellBG setBackgroundColor:[UIColor clearColor]];
    [cell setSelectedBackgroundView:selecetedCellBG ];
    
    }
    

    this SO 问题。

    【讨论】:

    • 您的答案效果很好,但是如果我们写入 cellForAtIndexPath 并且图像被覆盖,则此方法会被多次调用。如何只调用一次这个方法?
    • 这是单元格setSelectedBackgroundView 的唯一代码,您必须编写正确的创建单元格的代码,如yourcell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil){//create new}
    • 我写了这个但没有帮助,CRHomeCategCell *cell = (CRHomeCategCell *)[_tblCateg dequeueReusableCellWithIdentifier:CellIdentifier];if (cell == nil) {NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CRHomeCategCell" owner:self options:nil]; for (id oneObject in nib) if ([oneObject isKindOfClass:[CRHomeCategCell class]])cell = (CRHomeCategCell *)oneObject; [self setCellBGColorNormalAndSelectedForTableViewCell:cell cellForRowAtIndexPath:indexPath]; }cell.ImgCateg.image=[UIImage imageNamed:[ImgArr objectAtIndex:indexPath.row]];return cell;
    • @Krunal:请对其进行有问题的编辑,使其看起来正确,其他也可能有所帮助。
    【解决方案2】:

    您可以通过以下方式做到这一点:

    UIImage *cellImage = [UIImage imageNamed:@"myimage.png"];
    UIImageView *cellView = [[UIImageView alloc] initWithImage:cellImage];
    cellView.contentMode = UIContentViewModeScaleToFill; 
    cell.backgroundView = cellView;
    

    【讨论】:

      猜你喜欢
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 2020-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      相关资源
      最近更新 更多