Pregunta

Tengo UITableView en mi solicitud.

Atributos: TableStyle = Plain, SeperatorStyle = sola y SeperatorColor = negro

Lo que quiero hacer es que, quiero poner una imagen (que una pequeña franja), como separador de la tabla.

Por favor, ayúdame.

Saludos, Pratik

¿Fue útil?

Solución

Así que, por lo general con una mesa de iPhone, sólo debe utilizar uno de los construidos en los estilos: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html # // apple_ref / OCC / instp / UITableView / separatorStyle

Como un desarrollador de iPhone que ha pasado por esto, yo sugiero que evitar imágenes siempre que sea posible, y utilizar la API para construir su aplicación.

Si usted insiste en su curso imprudente, o si tiene un cliente exigente esto o algo, entonces lo que puede hacer es subclase UITableViewCell.

En la subclase UITableViewCell, se puede añadir lo que los elementos de interfaz de usuario que desee a la contentView de la célula, una imagen de separación en la parte inferior de la celda incluida.

Así pues, aquí es una implementación de la función de delegado que devuelve una celda mediante una costumbre UITableViewCell:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  static NSString *CellIdentifier = @"Cell";

  // notice I use a SavedTableCell here instead of a UITavbleViewCell
  SavedTableCell *cell = (SavedTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (cell == nil) {
    cell = [[[SavedTableCell alloc] initWithReuseIdentifier:CellIdentifier hasIcon:YES] autorelease];
  }

  // custom function to set the fields in the cell
  [cell setItem:[self.items objectAtIndex:indexPath.row]];  
  return cell;
}

Y entonces aquí está mi aplicación simplificada de SavedTableCell.h:

#import <UIKit/UIKit.h>
#import "Item.h"

@interface SavedTableCell : UITableViewCell {

  // my cell has one label and one image, but yours would have an image placed at the bottom of the cell's frame
  UILabel *primaryLabel;
  UIImageView *icon;
}

// i just made up the class Item... in my code they are actually Waypoints.
- (void) setItem:(Item*)item;


@property(nonatomic,retain) UILabel *primaryLabel;
@property(nonatomic,retain) UIImageView *icon;


@end

Y aquí es un SavedTableCell.m simplificada:

#import "SavedTableCell.h"

@implementation SavedTableCell

@synthesize primaryLabel, icon;


- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier {
  if (self = [super initWithStyle:UITableViewStylePlain reuseIdentifier:reuseIdentifier]) {
    icon = [[UIImageView alloc] initWithFrame:CGRectMake(5,5,40,40)];
    [self.contentView addSubview:icon];
    primaryLabel = [[UILabel alloc]init];
primaryLabel.font = TITLE_FONT;     
    [self.contentView addSubview:primaryLabel]; 
  } 
  return self;
}


- (void) setItem:(Item*)item {
  self.primaryLabel.text = [item name];
  [self.icon setImage:[item imageName]];
}

- (void)layoutSubviews {
  [super layoutSubviews];
  primaryLabel.frame = LABEL_FRAME;
  icon.frame = ICON_FRAME;  
}

- (void)dealloc {
  [icon release];
  [primaryLabel release];
}


@end

Otros consejos

solución que funcionó para mí.

self.tableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage"]];

quiero sólo para añadir algunas reflexiones a la respuesta de Andrew Johnson.

Si se agrega a subvista contentView y utilizar accesoryView o imagen, entonces se va a mostrar la imagen separador de forma incorrecta. Usar algo como

UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)];
    bg.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.2];
                    //or [UIColor colorWithImagePattern];
                    //or crete UIImageView and assign to backgroundView of the cell
self.backgroundView = bg;
[bg release];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top