2010-11-27 67 views
0


有3个设置,在我的应用程序,可以更改单元格的绘图。
默认情况下,我的表格视图中的单元格显示对象的名称和成本...更改这3个设置,用户可以选择显示说明或成本链接。
我写了很多代码,现在,我的应用程序可以更改单元格的绘图而不会退出它...
我的问题是绘图仅针对添加的新对象进行更改,但旧对象不会更改!
我怎样才能改变老细胞的绘图而不退出应用程序?自定义单元格与drawRect:和setNeedsDisplay方法

这是我的手机的代码(我用setNeedsDisplay和drawRect中的方法):


#import "WishTableCell.h" 


@implementation WishTableCell 

@synthesize wish; 
@synthesize imageView; 
@synthesize nomeLabel; 
@synthesize label; 
@synthesize costoLabel; 
@synthesize linkDescLabel; 


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { 

     imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 11, 28, 28)]; 
     imageView.contentMode = UIViewContentModeCenter; 
     [self.contentView addSubview:imageView]; 

     nomeLabel = [[UILabel alloc] initWithFrame:CGRectMake(58, 8, 235, 22)]; 
     [self.contentView addSubview:nomeLabel]; 

     if ([NSLocalizedString(@"CostoCella", @"") isEqualToString:@"Costo:"]) { 
      label = [[UILabel alloc] initWithFrame:CGRectMake(58, 28, 40, 16)]; 
     } 
     else { 
      label = [[UILabel alloc] initWithFrame:CGRectMake(58, 28, 35, 16)]; 
     } 
     [self.contentView addSubview:label]; 

     if ([NSLocalizedString(@"CostoCella", @"") isEqualToString:@"Costo:"]) { 
      costoLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 28, 185, 16)]; 
     } 
     else { 
      costoLabel = [[UILabel alloc] initWithFrame:CGRectMake(93, 28, 195, 16)]; 
     } 
     [self.contentView addSubview:costoLabel]; 

     linkDescLabel = [[UILabel alloc] initWithFrame:CGRectMake(58, 28, 235, 16)]; 
     [self.contentView addSubview:linkDescLabel]; 

     self.backgroundView = [[UIImageView alloc] init]; 
     UIImage *rowBackground = [UIImage imageNamed:@"cellBg.png"]; 
     ((UIImageView *)self.backgroundView).image = rowBackground; 
    } 

    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 

    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

-(void)setWish:(Wish *)newWish { 
    if (newWish != wish) { 
     [wish release]; 
     wish = [newWish retain]; 
    } 

    [self setNeedsDisplay]; 
} 

-(void)drawRect:(CGRect)rect { 

    NSLog(@"DrawRect called!"); 

    nomeLabel.text = wish.nome; 
    nomeLabel.font = [UIFont boldSystemFontOfSize:18.0]; 
    nomeLabel.textColor = [UIColor colorWithRed:0.039 green:0.4 blue:0.737 alpha:1.0]; 
    nomeLabel.textAlignment = UITextAlignmentLeft; 
    nomeLabel.shadowColor = [UIColor whiteColor]; 
    nomeLabel.shadowOffset = CGSizeMake(0, 1); 
    nomeLabel.backgroundColor = [UIColor clearColor]; 

    label.font = [UIFont boldSystemFontOfSize:12.0]; 
    label.text = NSLocalizedString(@"CostoCella", @""); 
    label.textColor = [UIColor colorWithRed:0.262 green:0.258 blue:0.258 alpha:1.0]; 
    label.textAlignment = UITextAlignmentLeft; 
    label.shadowColor = [UIColor whiteColor]; 
    label.shadowOffset = CGSizeMake(0, 1); 
    label.backgroundColor = [UIColor clearColor]; 

    costoLabel.font = [UIFont boldSystemFontOfSize:12.0]; 
    costoLabel.textColor = [UIColor colorWithRed:0.262 green:0.258 blue:0.258 alpha:1.0]; 
    costoLabel.textAlignment = UITextAlignmentLeft; 
    costoLabel.shadowColor = [UIColor whiteColor]; 
    costoLabel.shadowOffset = CGSizeMake(0, 1); 
    costoLabel.backgroundColor = [UIColor clearColor]; 

    linkDescLabel.font = [UIFont boldSystemFontOfSize:12.0]; 
    linkDescLabel.textColor = [UIColor colorWithRed:0.262 green:0.258 blue:0.258 alpha:1.0]; 
    linkDescLabel.textAlignment = UITextAlignmentLeft; 
    linkDescLabel.shadowColor = [UIColor whiteColor]; 
    linkDescLabel.shadowOffset = CGSizeMake(0, 1); 
    linkDescLabel.backgroundColor = [UIColor clearColor]; 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

    if ([[defaults objectForKey:@"dettagliView"] isEqualToString:@"costoView"]) { 

     linkDescLabel.hidden = YES; 
     label.hidden = NO; 
     costoLabel.hidden = NO; 

     if ([[defaults objectForKey:@"valutaCosto"] isEqualToString:@"Euro"]) { 
      NSString *costo = [[NSString alloc] initWithFormat:@"€ %@", wish.costo]; 
      costoLabel.text = costo; 
     } 
     if ([[defaults objectForKey:@"valutaCosto"] isEqualToString:@"Dollaro"]) { 
      NSString *costo = [[NSString alloc] initWithFormat:@"$ %@", wish.costo]; 
      costoLabel.text = costo; 
     } 
     if ([[defaults objectForKey:@"valutaCosto"] isEqualToString:@"Sterlina"]) { 
      NSString *costo = [[NSString alloc] initWithFormat:@"£ %@", wish.costo]; 
      costoLabel.text = costo; 
     } 
    } 
    else if ([[defaults objectForKey:@"dettagliView"] isEqualToString:@"descrizioneView"]) { 

     label.hidden = YES; 
     costoLabel.hidden = YES; 
     linkDescLabel.hidden = NO; 

     linkDescLabel.text = wish.descrizione; 
    } 
    else if ([[defaults objectForKey:@"dettagliView"] isEqualToString:@"urlView"]) { 

     label.hidden = YES; 
     costoLabel.hidden = YES; 
     linkDescLabel.hidden = NO; 

     linkDescLabel.text = wish.link; 
    } 

    if (wish.categoria == nil) 
     imageView.image = [UIImage imageNamed:@"personale.png"]; 

    if ([wish.categoria isEqualToString:@"Abbigliamento"]) 
     imageView.image = [UIImage imageNamed:@"abbigliamento.png"]; 

    else if ([wish.categoria isEqualToString:@"Casa"]) 
     imageView.image = [UIImage imageNamed:@"casa.png"]; 

    else if ([wish.categoria isEqualToString:@"Cibo"]) 
     imageView.image = [UIImage imageNamed:@"cibo.png"]; 

    else if ([wish.categoria isEqualToString:@"Divertimento"]) 
     imageView.image = [UIImage imageNamed:@"divertimento.png"]; 

    else if ([wish.categoria isEqualToString:@"Elettronica"]) 
     imageView.image = [UIImage imageNamed:@"elettronica.png"]; 

    else if ([wish.categoria isEqualToString:@"Hobby"]) 
     imageView.image = [UIImage imageNamed:@"hobby.png"]; 

    else if ([wish.categoria isEqualToString:@"Internet"]) 
     imageView.image = [UIImage imageNamed:@"internet.png"]; 

    else if ([wish.categoria isEqualToString:@"Regali"]) 
     imageView.image = [UIImage imageNamed:@"regali.png"]; 

    else if ([wish.categoria isEqualToString:@"Ufficio"]) 
     imageView.image = [UIImage imageNamed:@"ufficio.png"]; 

    else if ([wish.categoria isEqualToString:@"Viaggi"]) 
     imageView.image = [UIImage imageNamed:@"viaggi.png"]; 

    else if ([wish.categoria isEqualToString:@"Personale"]) 
     imageView.image = [UIImage imageNamed:@"personale.png"]; 
} 


- (void)dealloc { 
    [wish release]; 
    [imageView release]; 
    [nomeLabel release]; 
    [costoLabel release]; 
    [linkDescLabel release]; 
    [label release]; 
    [super dealloc]; 
} 


@end

非常感谢您的关注!
Matthew

+0

是否有任何人能帮助我吗? – matteodv 2010-11-27 22:18:40

回答

0

尝试调用[NSTableView reloadData]函数,并在委托方法(cellForRowAtIndexPath)中设置绘图属性。

+0

我在cellForRowAtIndexPath方法中使用此代码:http://is.gd/hU5vU我该怎么办? – matteodv 2010-11-28 08:32:13

+0

当一些绘制属性发生变化时,调用一个reloadData,它将重新加载整个表格并更新单元格。 – MetaImi 2010-11-28 10:53:01

0

我认为你也可以用接口来创建单元格(的.xib),那么你就不需要写那么多指派属性代码....

相关问题