54

セルの左側ではなく右側に表示するように UITableViewCell.image を設定する方法はありますか? または、セル レイアウトの右側に別の UIImageView を追加する必要がありますか?

4

11 に答える 11

91

いいえ。ただし、同じ効果を得るために、画像ビューをアクセサリ ビューとしてテーブル セルに簡単に追加できます。

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]];
cell.accessoryView = imageView;
[imageView release];
于 2009-04-23T17:12:27.983 に答える
8

を使用するアプローチを使用した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!
}
于 2015-04-25T23:53:12.160 に答える
5

カスタム セルを作成せず、標準のセルで動作する場合は、少なくとも 2 つの方法があります。

  1. アクセサリビューを使用するには。
cell.accessoryView = UIImageView(image: UIImage(named: "imageName"))
cell.accessoryView.frame = CGRectMake(0, 0, 22, 22)
  1. 右の画像+accessoryViewが必要な場合は、UIImageViewアウトレットをContentView(例の名前:rightImage)に追加し、textLabelの背景色をClearに変更します。
cell.rightImage.image = UIImage(named: "imageName")
cell.textLabel.backgroundColor = UIColor.clearColor()
于 2015-07-30T05:39:28.847 に答える
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
于 2012-04-17T14:01:00.223 に答える
0

セルの右側に画像を追加するには、右側に画像ビューを持つカスタムセルを作成することをお勧めします。これは非常に便利です。空のビューを追加して、この nib を customcell クラスにリンクします。

セルのnibファイルでビューを削除し、tableViewcellを追加し、そのセルでimageViewを右側にドラッグアンドドロップします。

次に、 DemoCell と言う TableViewCell のクラスに移動し、アウトレットを作成し、テーブルセルの nib に 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 ....

于 2012-07-12T09:45:21.407 に答える
0

独自の画像を作成する必要はありません。編集するだけですcell.tintColor

于 2015-07-14T07:49:38.273 に答える
0

[imageView setFrame:CGRectMake(0, 0, 35.0, 35.0)]; [imageView setFrame:CGRectMake(0, 0, 35.0, 35.0)];

于 2013-07-22T19:31:55.780 に答える
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)

これにより、画像がセルの右側に配置されます。このソリューションは、画面サイズに応じて位置を自動的に調整しています。唯一の欠点は、デバイスを回転させるときにデータをリロードする必要があることですがoverrideUITableViewクラスでdidRotateリスナー メソッドを使用できます。

override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
    _tableView.reloadData()}
于 2018-01-18T23:17:34.817 に答える
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"];
}
于 2010-03-01T19:05:43.177 に答える