私のアプリには、セルの描画を変更できる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