2009-04-23 90 views

回答

74

不,但您可以轻松地将图像视图作为附件视图添加到表格单元中,以获得相同效果。

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]]; 
cell.accessoryView = imageView; 
[imageView release]; 
+1

将改变配件视图,然后覆盖披露指标? – 2009-04-23 21:20:04

+0

是的,它会的。如果你想要,那么你必须在创建时手动添加一个UIImageView到你的单元格。或者为你的特殊表格单元格创建一个子类,它可以很好地包装你。 – 2009-04-23 23:46:34

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"]; 
} 
2

子类的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 
0

要在单元格@右侧添加图片,我会建议你要在右边用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 ....

0

可以RESI通过调用imageview的setFrame方法,在accessoryview中显示ze图像:[imageView setFrame:CGRectMake(0,0,35.0,35.0)];

5

这里是斯威夫特使用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! 
} 
0

这是不必要的创建自己的图像,只需编辑cell.tintColor

10

对于简单的情况下,你可以这样做:

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和任何文本标签,否则他们自己将被镜像!该解决方案保留了右侧的附件视图。

4

如果你不想做一个自定义单元格,将与标准的一个工作,你必须作为最小的方式有两种:

  1. 要使用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() 
    
    0

    有设置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) 
    

    这将在单元格的右侧位置图像。 该解决方案根据屏幕大小自动调整位置。 唯一的缺点是,当你旋转你需要重新加载数据的设备,但你只是overrideUITableViewdidRotate监听方法可以:

    override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) { 
        _tableView.reloadData()} 
    
    0

    swift3swift4,我们可以使用这个:

    cell.accessoryView = UIImageView(image:UIImage(named:"imageNmae")!)