有没有办法将UITableViewCell.image设置为在单元格的右侧而不是左侧显示?或者我需要在单元布局的右侧添加单独的UIImageView?UITableViewCell与图像在右边?
回答
不,但您可以轻松地将图像视图作为附件视图添加到表格单元中,以获得相同效果。
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]];
cell.accessoryView = imageView;
[imageView release];
这SOLN是在每个小区中加入不同的图像....只是一种尝试
// Customize the appearance of table view cells.
- (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];
}
if(indexPath.row == 0) {
cell.image = [UIImage imageNamed:@"image.png"];
}
else if (indexPath.row == 1) {
cell.image = [UIImage imageNamed:@"image1.png"];
}
子类的UITableViewCell并重写layoutSubviews,然后只调整self.textLabel的帧, self.detailTextLabel和self.imageView视图。
这是怎么看起来像在代码:
MYTableViewCell.h
:
#import <UIKit/UIKit.h>
@interface MYTableViewCell : UITableViewCell
@end
MYTableViewCell.m
:
#import "MYTableViewCell.h"
@implementation MYTableViewCell
- (void)layoutSubviews
{
[super layoutSubviews];
if (self.imageView.image) {
self.imageView.frame = CGRectMake(CGRectGetWidth(self.contentView.bounds) - 32 - 8,
CGRectGetMidY(self.contentView.bounds) - 16.0f,
32,
32);
CGRect frame = self.textLabel.frame;
frame.origin.x = 8;
self.textLabel.frame = frame;
frame = self.detailTextLabel.frame;
frame.origin.x = 8;
self.detailTextLabel.frame = frame;
}
}
@end
要在单元格@右侧添加图片,我会建议你要在右边用imageview创建一个自定义单元格,它对你来说非常有用。 并添加一个空的视图来将此笔尖与customcell类连接起来。
现在在你的单元格的nib文件中删除视图,添加tableViewcell并在该单元格中拖动一个imageView到右侧。
现在去TableViewCell的类说DemoCell,形成一出口,并与在的TableCell的笔尖ImageView的设置现在
,在tableview中的的cellForRowAtIndexPath方法引用您的细胞,并用这样的
笔尖名称中使用它static NSString *CellIdentifier = @"Cell";
DemoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[SettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell = [[[NSBundle mainBundle]loadNibNamed:@"DemoCell" owner:self options:nil] lastObject];
}
,并设置图像按照您的要求
cell.imageVw.img ....
可以RESI通过调用imageview的setFrame方法,在accessoryview中显示ze图像:[imageView setFrame:CGRectMake(0,0,35.0,35.0)];
这里是斯威夫特使用accessoryView
的方法的例子:
private let reuseIdentifier: String = "your-cell-reuse-id"
// MARK: UITableViewDataSource
func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as? UITableViewCell
if (cell == nil) {
cell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:reuseIdentifier)
}
cell!.textLabel!.text = "Hello"
cell!.detailTextLabel!.text = "World"
cell!.accessoryView = UIImageView(image:UIImage(named:"YourImageName")!)
return cell!
}
这是不必要的创建自己的图像,只需编辑cell.tintColor
。
对于简单的情况下,你可以这样做:
cell.contentView.transform = CGAffineTransformMakeScale(-1,1);
cell.imageView.transform = CGAffineTransformMakeScale(-1,1);
cell.textLabel.transform = CGAffineTransformMakeScale(-1,1);
cell.textLabel.textAlignment = NSTextAlignmentRight; // optional
这将翻转(镜像)将任何ImageView的右侧的内容视图。请注意,你必须翻转imageView和任何文本标签,否则他们自己将被镜像!该解决方案保留了右侧的附件视图。
如果你不想做一个自定义单元格,将与标准的一个工作,你必须作为最小的方式有两种:
- 要使用accessoryView的。
cell.accessoryView = UIImageView(image: UIImage(named: "imageName"))
cell.accessoryView.frame = CGRectMake(0, 0, 22, 22)
- 如果想要有右图像+ accessoryView的,那么就可以的UIImageView出口添加到内容查看(例如名称:右图)和改变为textLabel的背景色明确。
cell.rightImage.image = UIImage(named: "imageName")
cell.textLabel.backgroundColor = UIColor.clearColor()
有设置position
编程的方式。这里是Swift
版本的解决方案:
image.frame = CGRect.init(
x: self.view.frame.width - image.frame.width*1.1,
y: image.frame.origin.y,
width: image.frame.width,
height: image.frame.height)
这将在单元格的右侧位置图像。 该解决方案根据屏幕大小自动调整位置。 唯一的缺点是,当你旋转你需要重新加载数据的设备,但你只是override
在UITableView
类didRotate
监听方法可以:
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
_tableView.reloadData()}
在swift3和swift4,我们可以使用这个:
cell.accessoryView = UIImageView(image:UIImage(named:"imageNmae")!)
- 1. UITableViewCell与右侧图像
- 2. 把背景图像放在右边10px边框带边框
- 3. Android:酒吧与中心图像和按钮在右边
- 4. UITableViewCell与图像滞后
- 5. UITableViewCell右侧的边框不可见
- 6. Android布局文本在左边,图像在右边
- 7. 在图像上显示一个列表:文本在图像的右边
- 8. mogrify增加100像素的jpg图片的左边和右边?
- 9. 引导左,右页边距到图像的边缘
- 10. 在UITableViewCell中保存图像
- 11. 在UITableViewCell中放大图像?
- 12. Div与边框图像
- 13. UITableViewCell有方形角与图像
- 14. HTML日历:底部图像显示在右边不在底部
- 15. 在UITableViewCell的边界之外绘图
- 16. 计算边界图像宽度与边界图像首先
- 17. 在UITableViewCell上向右滑动
- 18. 如何将UITableViewCell图像移动到右侧
- 19. NSButton与右边对齐
- 20. UITableViewCell加载图像
- 21. uitableviewcell调整图像
- 22. 如果图像在右边,我怎么能将文字换行?
- 23. 如何在CSS溢出DIV中默认图像的右边缘?
- 24. 在IE7中图像不会正确浮动到右边
- 25. 在Delphi/Win32中将图像放入组合框(右侧边缘)
- 26. 添加到Storyboard按钮的图像没有出现在右边
- 27. Android LinearLayout,无法让图像出现在LinearLayout的右边
- 28. 图片右手边打破
- 29. 左边和右边的div边界居中布局(带bg图像)(drupal)
- 30. 要在Y左边和Y右边重复的多个背景图像
将改变配件视图,然后覆盖披露指标? – 2009-04-23 21:20:04
是的,它会的。如果你想要,那么你必须在创建时手动添加一个UIImageView到你的单元格。或者为你的特殊表格单元格创建一个子类,它可以很好地包装你。 – 2009-04-23 23:46:34