有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
是否有任何人能幫助我嗎? – matteodv 2010-11-27 22:18:40