UITableViewCell z obrazem po prawej stronie?

Czy istnieje sposób na ustawienie UITableViewCell.obraz do wyświetlenia po prawej stronie komórki zamiast po lewej? Czy też będę musiał dodać oddzielny interfejs UIImageView po prawej stronie układu komórki?

Author: Oliver GL, 2009-04-23

11 answers

Nie. Możesz jednak łatwo dodać widok obrazu jako widok akcesoriów do komórki tabeli, aby uzyskać ten sam efekt.

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]];
cell.accessoryView = imageView;
[imageView release];
 77
Author: Alex Wayne,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-21 04:44:15

W prostych przypadkach możesz to zrobić:

cell.contentView.transform = CGAffineTransformMakeScale(-1,1);
cell.imageView.transform = CGAffineTransformMakeScale(-1,1);
cell.textLabel.transform = CGAffineTransformMakeScale(-1,1);
cell.textLabel.textAlignment = NSTextAlignmentRight; // optional

Spowoduje to odwrócenie (odbicie lustrzane) widoku zawartości umieszczając dowolny widok imageView po prawej stronie. Zauważ, że musisz odwrócić imageView i wszelkie etykiety tekstowe, w przeciwnym razie same zostałyby odbite! Takie rozwiązanie pozwala zachować widok akcesoriów po prawej stronie.

 10
Author: TomSwift,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-24 17:44:59

Oto przykład w Swift z podejściem użycia 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!
}
 6
Author: Zorayr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-25 23:53:12

Jeśli nie chcesz tworzyć niestandardowej komórki i będziesz pracować ze standardową, masz co najmniej dwa sposoby:

  1. aby użyć accessoryView.
cell.accessoryView = UIImageView(image: UIImage(named: "imageName"))
cell.accessoryView.frame = CGRectMake(0, 0, 22, 22)
  1. Jeśli chcesz mieć odpowiedni obraz + accessoryView, możesz dodać Ujście UIImageView do ContentView (przykładowa nazwa: rightImage) I zmienić kolor tła etykiety textLabel jako przezroczysty.
cell.rightImage.image = UIImage(named: "imageName")
cell.textLabel.backgroundColor = UIColor.clearColor()
 4
Author: gerram,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-30 06:50:43

Podklasa UITableViewCell i override layoutSubviews a następnie po prostu dostosuj ramki jaźni.textLabel, self.detailTextLabel i ja.widoki imageView.

Tak to może wyglądać w kodzie:

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
 2
Author: Camsoft,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-11 17:47:36

W swift3 i swift4 możemy użyć tego:

cell.accessoryView = UIImageView(image:UIImage(named:"imageNmae")!)
 1
Author: Mohsen mokhtari,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-13 14:39:18

Aby dodać obrazek @ po prawej stronie w komórce, sugerowałbym utworzenie niestandardowej komórki z imageview po prawej stronie , będzie to bardzo przydatne dla Ciebie . i dodaj pusty widok, aby połączyć tę stalówkę z klasą customcell.

Teraz w pliku nib komórki Usuń widok, dodaj tableViewcell i w tej komórce przeciągnij i upuść imageView po prawej stronie.

Teraz przejdź do klasy TableViewCell, powiedz DemoCell, Utwórz wylot i ustaw go z imageview w obrusie stalówka

Teraz, w metodzie tableview cellforrowatindexpath odwołaj się do komórki i użyj jej z nazwą nib w ten sposób

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

I ustaw obraz zgodnie z wymaganiami

Komórka.imageVw.img ....

 0
Author: iCoder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-12 09:45:21

Możesz zmienić rozmiar obrazu w accessoryview, wywołując metodę setFrame imageview w następujący sposób: [imageView setFrame: CGRectMake(0, 0, 35.0, 35.0)];

 0
Author: user2205998,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-22 19:31:55

Ten Soln służy do dodawania innego obrazu w każdej komórce....Just a Try

// 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"];
}
 0
Author: user283846,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-25 23:48:52

Nie trzeba tworzyć własnego obrazu, wystarczy edytować cell.tintColor.

 0
Author: samthui7,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-14 07:49:38

Istnieje sposób, aby ustawić position programowo. Oto wersja Swift rozwiązania:

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)

To ustawi obraz po prawej stronie komórki. To rozwiązanie automatycznie dostosowuje pozycję w zależności od rozmiaru ekranu. Jedynym minusem jest to, że gdy obracasz urządzenie, musisz przeładować dane, ale możesz po prostu override w swojej klasie UITableView metoda didRotate listener:

override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
    _tableView.reloadData()}
 0
Author: EranKT,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-18 23:17:34