セルの左側ではなく右側に表示するように UITableViewCell.image を設定する方法はありますか? または、セル レイアウトの右側に別の UIImageView を追加する必要がありますか?
11 に答える
いいえ。ただし、同じ効果を得るために、画像ビューをアクセサリ ビューとしてテーブル セルに簡単に追加できます。
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]];
cell.accessoryView = imageView;
[imageView release];
を使用するアプローチを使用した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!
}
カスタム セルを作成せず、標準のセルで動作する場合は、少なくとも 2 つの方法があります。
- アクセサリビューを使用するには。
cell.accessoryView = UIImageView(image: UIImage(named: "imageName"))
cell.accessoryView.frame = CGRectMake(0, 0, 22, 22)
- 右の画像+accessoryViewが必要な場合は、UIImageViewアウトレットをContentView(例の名前:rightImage)に追加し、textLabelの背景色をClearに変更します。
cell.rightImage.image = UIImage(named: "imageName")
cell.textLabel.backgroundColor = UIColor.clearColor()
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を右側にドラッグアンドドロップします。
次に、 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 ....
独自の画像を作成する必要はありません。編集するだけですcell.tintColor
。
[imageView setFrame:CGRectMake(0, 0, 35.0, 35.0)]; [imageView setFrame:CGRectMake(0, 0, 35.0, 35.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)
これにより、画像がセルの右側に配置されます。このソリューションは、画面サイズに応じて位置を自動的に調整しています。唯一の欠点は、デバイスを回転させるときにデータをリロードする必要があることですがoverride
、UITableView
クラスでdidRotate
リスナー メソッドを使用できます。
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
_tableView.reloadData()}
この 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"];
}