【发布时间】:2010-10-21 07:00:22
【问题描述】:
有没有办法将 UITableViewCell.image 设置为显示在单元格的右侧而不是左侧?或者我需要在单元格布局的右侧添加一个单独的 UIImageView 吗?
【问题讨论】:
标签: iphone objective-c uitableview uiimageview uiimage
有没有办法将 UITableViewCell.image 设置为显示在单元格的右侧而不是左侧?或者我需要在单元格布局的右侧添加一个单独的 UIImageView 吗?
【问题讨论】:
标签: iphone objective-c uitableview uiimageview uiimage
没有。但是您可以轻松地将图像视图作为附件视图添加到表格单元格以获得相同的效果。
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]];
cell.accessoryView = imageView;
[imageView release];
【讨论】:
[imageView sizeToFit];,希望这会有所帮助:)
对于简单的情况,您可以这样做:
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 和任何文本标签,否则它们本身将被镜像!此解决方案保留右侧的附件视图。
【讨论】:
这是 Swift 中使用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.accessoryView = UIImageView(image: UIImage(named: "imageName"))
cell.accessoryView.frame = CGRectMake(0, 0, 22, 22)
cell.rightImage.image = UIImage(named: "imageName")
cell.textLabel.backgroundColor = UIColor.clearColor()
【讨论】:
在 swift3 和 swift4 中,我们可以使用这个:
cell.accessoryView = UIImageView(image:UIImage(named:"imageNmae")!)
【讨论】:
继承 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
【讨论】:
要在单元格中添加图像@右侧,我建议您创建一个自定义单元格,右侧带有图像视图,这对您非常有用。 并添加一个空视图以将此 nib 与 customcell 类链接。
现在在您的单元格的 nib 文件中删除视图,添加 tableViewcell 并在该单元格中将 imageView 拖放到右侧。
现在去 TableViewCell 的类说 DemoCell ,创建一个 outlet 并使用 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 ....
【讨论】:
您可以通过调用 imageview 的 setFrame 方法来调整附件视图中的图像大小,如下所示:[imageView setFrame:CGRectMake(0, 0, 35.0, 35.0)];
【讨论】:
此 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"];
}
【讨论】:
无需创建自己的图片,只需编辑cell.tintColor即可。
【讨论】:
有一种方法可以以编程方式设置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)
这会将您的图像放置在单元格的右侧。
该解决方案是根据屏幕尺寸自动调整位置。
唯一的缺点是当您旋转设备时需要重新加载数据,但您可以在 UITableView 类中使用 override didRotate 侦听器方法:
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
_tableView.reloadData()}
【讨论】: