【问题标题】:Set image on Table View Right corner在表格视图右上角设置图像
【发布时间】:2013-08-08 13:15:01
【问题描述】:

如何在 Table View 右上角设置 UIImage。我正在使用表格视图和显示联系人列表做 iPad 应用程序。单击联系人表格视图时要显示右侧详细信息...在这里我想放置一张在表格视图中选择的图像...

谁给我这个建议

谢谢

在这里查看我的代码

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
               ![enter image description here][1]

         UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(347, 0.5, 14, 48)];
         imv.image=[UIImage imageNamed:@"activeBg.png"];
         [cell.selectedBackgroundView addSubview:imv];


}

预期输出:

我的输出:

使用过的图片:

更改 x 值后看到此输出:

【问题讨论】:

  • 更改xCGRectMake(347, 0.5, 14, 48) 的值,可能是355
  • CGRectMake(352, 0.5, 14, 48) 我使用了这个代码
  • 嘿@Swathi 检查我的答案,........
  • 您的视图正在剪切它。 [view setClipsToBounds:NO]; 为您提供所有视图层次结构

标签: ios objective-c ipad ios5


【解决方案1】:

viewDidLoad:方法中写下这两行。

arrowImage = [UIImage imageNamed:@"image"];
[tableView setClipsToBounds:NO];

现在你需要在didSelectRowAtIndexPath: 中做的是:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self addArrowToIndexPath:indexPath];
}

这里是方法定义:

-(void)addArrowToIndexPath:(NSIndexPath *)indexPath
{
    CGRect rect = [tableView rectForRowAtIndexPath:indexPath];

    if (arrowView)
    {
        [arrowView removeFromSuperview];
        arrowView = nil;
    }

    // arrowView is an UIImageView declared globally
    arrowView  = [[UIImageView alloc] initWithImage:arrowImage];
    rect.origin.x = CGRectGetMaxX(rect);
    rect.size = arrowImage.size;
    [arrowView setFrame:rect];
    [tableView addSubview:arrowView];
}

注意:还要设置行高 = arrowImage.size.height,即它可以适合单元格高度。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return arrowImage.size.height;
}

您可以直接下载sample

【讨论】:

    【解决方案2】:

    首先,要小心浮动坐标,因为它们可能看起来很模糊!

    你的 UITableViewCell 的宽度是多少?您应该根据您的单元格大小计算坐标:

    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
         CGRect rectImageView = CGRectMake(cell.frame.size.width - 14, 1, 14, 48); // maybe even y and height is calculateable
         UIImageView *imv = [[UIImageView alloc]initWithFrame:rectImageView];
         imv.image=[UIImage imageNamed:@"activeBg.png"];
         [cell.selectedBackgroundView addSubview:imv];
         [cell setClipsToBounds:NO]; // do not clip subviews
    
    
    }
    

    另外 setClipsToBounds 很重要,如果我没记错的话 YES 是默认值...

    【讨论】:

    • CGRect rectImageView = CGRectMake(cell.frame.size.width - 6, 1, 14, 48); // 甚至 y 和 height 都可以计算 UIImageView *imv = [[UIImageView alloc]initWithFrame:rectImageView]; imv.image=[UIImage imageNamed:@"activeBg.png"]; [cell.selectedBackgroundView addSubview:imv]; [单元格 setClipsToBounds:NO]; // 不要剪辑我使用过此代码的子视图,但图像角隐藏...
    • 您是否尝试过在表格视图和它的容器上设置 clipsToBounds = NO?
    【解决方案3】:

    用属性试试

    [view setClipsToBounds:NO];
    

    我不确定你必须在哪个设置这个 NO,但请尝试设置 self.view、UITableView 或 Cell..

    【讨论】:

      【解决方案4】:

      渐变背景是cell.backgroundView?看来您的 activeBg.png 在右侧是半透明的,您可以通过它看到 cell.backgroundView。我看到三个解决方案:

      1. 为渐变背景图像添加半透明条(如果您使用图像,但似乎您也可以使用 CoreGraphics 渐变)
      2. 渐变背景不应该是 cell.backgroundView,而是它的子视图,即较小的 cell.backgroundView(可能与您的 arroy 和 strench 图像一起使用)
      3. 重写cell的layoutSubviews方法,让cell.backgroundView的宽度更小(我觉得这样不好)

      【讨论】:

        猜你喜欢
        • 2016-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多