有没有一种方法可以将UITableViewCell.image设置为在单元格的右侧显示,而不是左侧?或者我需要在单元格布局的右侧添加一个单独的UIImageView吗?
有没有一种方法可以将UITableViewCell.image设置为在单元格的右侧显示,而不是左侧?或者我需要在单元格布局的右侧添加一个单独的UIImageView吗?
不行,但你可以将一个图片视图作为表格单元格的附件视图来轻松实现相同的效果。
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]];
cell.accessoryView = imageView;
[imageView release];
对于简单的情况,你可以这样做:
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
方法的 Swift 示例:
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
有一种方法可以通过编程方式设置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
类中覆盖
didRotate
监听器方法:
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
_tableView.reloadData()}
这个解决方案是为了在每个单元格中添加不同的图像...只是尝试
// 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"];
}
要在单元格的右侧添加图像,我建议您创建一个带有右侧图像视图的自定义单元格,这对您非常有用。并添加一个空视图以将此nib与customcell类链接。
现在,在您的单元格的nib文件中删除视图,添加tableViewcell,并在该单元格中拖放一个imageView到右侧。
现在进入TableViewCell的类DemoCell,创建一个outlet并将其设置为tablecell的nib中的imageview。
现在,在tableview的cellforrowatindexpath方法中引用您的单元格,并像这样使用它与nib名称一起。
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 ....
没有必要创建自己的图像,只需编辑cell.tintColor
。
[imageView sizeToFit];
,希望这能有所帮助 :) - Adam Purdie