使用drawRect:和setNeedsDisplay方法的自定义单元格

时间:2010-11-27 18:48:24

标签: iphone uitableview drawing drawrect


在我的应用程序中,有3个设置可以更改单元格的绘图 默认情况下,我的表格视图的单元格显示对象的名称和成本...更改这3个设置,用户可以选择显示描述或成本的链接。
我写了很多代码,现在,我的应用程序可以更改单元格的绘图而不会退出... 我的问题是图纸仅针对添加的新对象进行了更改,但旧对象不会更改! 如何在不退出应用程序的情况下更改旧单元格的绘图?

这是我的单元格的代码(我正在使用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

非常感谢您的关注! 马修

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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