具有drawRect:和setNeedsDisplay方法的自定义单元格

发布于 2024-10-04 22:33:00 字数 6932 浏览 0 评论 0原文


在我的应用程序中,有 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

非常感谢您的关注!
马修

there are 3 settings, in my app, that can be change the drawing of a cell.
By default, a cell of my table view show the name and the cost of an object... Changing these 3 setting, an user can choose to show a description or a link insted of cost.
I wrote a lot of code and now, my app can change cell's drawing without quit from it...
My problem is the drawing is changed only for new objects added but old objects don't change!
How can I do to change also the old cell's drawing without quit from app?

This is the code of my cell (i'm using setNeedsDisplay and drawRect methods):


#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

Thanks a lot for the attention!
Matthew

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£噩梦荏苒 2024-10-11 22:33:00

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

Try to call [NSTableView reloadData] function, and set the drawing properties in the delegate method (cellForRowAtIndexPath).

悲歌长辞 2024-10-11 22:33:00

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

I think that you also can use interface to create the cell (.xib), then you do not need write so many assign property code....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文