我使用UITableViewController
显示一些数据,我的表格有2个静态部分,每个部分有6个静态行。而且我在继承UITableViewCell
以添加3个标签和一个视图,在视图中,我只在其中一个部分的一个单元格中绘制箭头。UITableView滚动更改UITableViewCell里面的图像
这一切都很顺利。直到我向下滚动...然后箭头随机放置在其他单元格中,一旦我向后滚动箭头再次更改单元格...这也发生如果我尝试设置backgroundColor
只有一个单元格。一旦我向下滚动,原本已经有颜色的细胞不再有它,另一个看似随机的细胞现在具有这种颜色。
下面是我的一些代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
TableViewCells *cell = (TableViewCells *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TableViewCells alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(TableViewCells *)cell atIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 0:
{
if (indexPath.section == cotizacionIdeal)
{
cell.arrowImage = [UIImage imageNamed:@"arrowUp.png"];
[cell.nombre setText:@"Recursos"];
[cell.cantidad setText:[NSString stringWithFormat:@"%1.2f", [cotizacionPrevia.sueldoDeRecursos doubleValue]]];
[cell.porcentaje setText:[NSString stringWithFormat:@"%1.2f%@", [cotizacion.recursosPorcentaje doubleValue], @"%"]];
}
else
{
[cell.nombre setText:@"Recursos"];
[cell.cantidad setText:[NSString stringWithFormat:@"%1.2f", [cotizacionPrevia.sueldoDeRecursos doubleValue]]];
[cell.porcentaje setText:[Calculate calcPorcentaje:cotizacionPrevia.sueldoDeRecursos totalReal:self.totalReal]];
}
}
break;
case 1:
{
if (indexPath.section == cotizacionIdeal)
{
[cell.nombre setText:@"Comision de Venta"];
[cell.cantidad setText:[Calculate calcMonto:cotizacion.comisionPorcentaje total:self.totalIdeal]];
[cell.porcentaje setText:[NSString stringWithFormat:@"%1.2f%@", [cotizacion.comisionPorcentaje doubleValue], @"%"]];
}
else
{
[cell.nombre setText:@"Comision de Venta"];
[cell.cantidad setText:self.montoRealComision];
[cell.porcentaje setText:[NSString stringWithFormat:@"%1.2f%@", [cotizacion.comisionPorcentaje doubleValue], @"%"]];
}
}
UITableViewCell
类:
- (id)initWithFrame:(CGRect)frame cell:(TableViewCells *)cell
{
if (self = [super initWithFrame:frame]) {
_cell = cell;
//self.opaque = YES;
//self.backgroundColor = _cell.backgroundColor;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
[_cell.arrowImage drawAtPoint:CGPointMake(0, 0)];
}
@end
@implementation TableViewCells
@synthesize nombre;
@synthesize porcentaje;
@synthesize cantidad;
@synthesize arrowImage;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// Initialization code
UILabel *nombreLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.nombre = nombreLabel;
[nombreLabel release];
[self.nombre setFont:[UIFont boldSystemFontOfSize:14]];
[self.contentView addSubview:self.nombre];
UILabel *porcentajeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.porcentaje = porcentajeLabel;
[porcentajeLabel release];
[self.porcentaje setFont:[UIFont italicSystemFontOfSize:10]];
[self.contentView addSubview:self.porcentaje];
UILabel *cantidadLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.cantidad = cantidadLabel;
[self.cantidad setTextAlignment:UITextAlignmentRight];
[cantidadLabel release];
[self.cantidad setFont:[UIFont boldSystemFontOfSize:14]];
[self.contentView addSubview:self.cantidad];
cellContentView = [[TableViewCellContentView alloc] initWithFrame:CGRectZero cell:self];
cellContentView.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:cellContentView];
}
UPDATE:
我自己也尝试设置图像一个不同除了一个人以外,每个人都有一个,我仍然有同样的问题。
- (void)configureCell:(TableViewCells *)cell atIndexPath:(NSIndexPath *)indexPath {
cell.arrowImage = [UIImage imageNamed:@"arrowDown.png"];
switch (indexPath.row) {
case 0:
{
if (indexPath.section == cotizacionIdeal)
{
cell.arrowImage = [UIImage imageNamed:@"arrowUp.png"];
[cell.nombre setText:@"Recursos"];
[cell.cantidad setText:[NSString stringWithFormat:@"%1.2f", [cotizacionPrevia.sueldoDeRecursos doubleValue]]];
[cell.porcentaje setText:[NSString stringWithFormat:@"%1.2f%@", [cotizacion.recursosPorcentaje doubleValue], @"%"]];
}
else
{
你好本,谢谢你的回答,我知道,视单元正在被重用,我试图明确清除cell.arrowImage属性设置为零。然而,这不太好。 即使我为每个单元格设置不同的图像,一旦向下滚动,图像就会混淆。我在每个单元格中设置图像的方式与第一个单元格相同。我应该如何处理这个问题?先谢谢你。 -Oscar – 2009-12-03 18:55:24
我还试图此形式给出 - (无效)configureCell:(TableViewCells *)细胞atIndexPath:(NSIndexPath *)indexPath { \t cell.arrowImage = [UIImage的imageNamed:@ “arrowDown.png”]; \t开关(indexPath.row){ \t \t情况下0: \t \t { \t \t \t如果(indexPath。部== cotizacionIdeal) \t \t \t { \t \t \t \t cell.arrowImage = [UIImage的imageNamed:@ “arrowUp.png”]; \t \t \t \t [cell.nombre setText:@“Recursos”]; \t \t \t \t [cell.cantidad setText:[NSString stringWithFormat:@“%1.2f”,[cotizacionPrevia.sueldoDeRecursos doubleValue]]]]; 这工作正常,直到我向下滚动。 – 2009-12-03 18:59:25
我想知道这是因为我使用iPhone模拟器? – 2009-12-03 19:32:22