如何在 Table View 右上角设置 UIImage。我正在使用表格 View 和显示联系人列表来制作 iPad 应用程序。单击联系人表格 View 时想要显示右侧详细信息...在这里我想放置一张在表格 View 中选择的图像...
有人给我建议吗
谢谢
在这里查看我的代码
-(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 值后查看此输出:
最佳答案
在 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 .
关于ios - 在表格 View 右上角设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18098685/