UITableViewCell与自定义单元格中的备用背景颜色 [英] UITableViewCell with alternate background color in customized cells

查看:73
本文介绍了UITableViewCell与自定义单元格中的备用背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的UITableViewCells的背景,每显示两个单元格有不同的颜色,但当我向下和向后滚动,他们都得到相同的颜色。如何知道我的单元格具有不同的contentView大小(根据其内容),我可以获得此效果?

I'd like the background to of my UITableViewCells to have a different color every two cells displayed, but when I scroll down and back, they all get the same color. How can I get this effect knowing that my cells have different contentView size (according to their content) ?

#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 20.0f
#define NAME_CELL_HEIGHT 20.0f

#import "CartCell.h"

@implementation CartCell

@synthesize nameLabel = _nameLabel;
@synthesize ingredientsLabel = _ingredientsLabel;
@synthesize myStore;

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

    myStore = [Store sharedStore];

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

        self.nameLabel = nil;
        self.ingredientsLabel = nil;

        // SET "NAME" CELL
        self.nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.nameLabel setLineBreakMode:UILineBreakModeWordWrap];
        [self.nameLabel setMinimumFontSize:FONT_SIZE];
        [self.nameLabel setNumberOfLines:1];
        [self.nameLabel setTag:1];
        self.nameLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
        [self.nameLabel sizeToFit];
        self.nameLabel.backgroundColor = [UIColor clearColor];
        [[self contentView] addSubview:self.nameLabel];

        // SET "INGREDIENTS" CELL
        self.ingredientsLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self.ingredientsLabel setLineBreakMode:UILineBreakModeWordWrap];
        [self.ingredientsLabel setMinimumFontSize:FONT_SIZE];
        [self.ingredientsLabel setNumberOfLines:0];
        [self.ingredientsLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]];
        [self.ingredientsLabel setTag:2];
        self.ingredientsLabel.backgroundColor = [UIColor clearColor];
        [[self contentView] addSubview:self.ingredientsLabel];

        if (myStore.cellBackgroundShouldBeLight == YES) {
            NSLog(@"clear [in] ? %@", myStore.cellBackgroundShouldBeLight ? @"Yes" : @"No");
            self.contentView.backgroundColor = [[UIColor alloc]initWithRed:87.0/255.0 green:168.0/255.0 blue:229.0/255.0 alpha:1];
            myStore.cellBackgroundShouldBeLight = NO;
        } else {
            NSLog(@"clear [in] ? %@", myStore.cellBackgroundShouldBeLight ? @"Yes" : @"No");
            self.contentView.backgroundColor = [[UIColor alloc]initWithRed:187.0/255.0 green:268.0/255.0 blue:229.0/255.0 alpha:1];
            myStore.cellBackgroundShouldBeLight = YES;
        }

    }

    return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

UPDATE:

我知道试图根据建议在cellForRowAtIndexPath中设置它,但我得到相同的结果:第一次滚动下来工作正常,但是然后再次向上滚动细胞背景颜色。

I'm know trying to set it in cellForRowAtIndexPath as it was suggested, but I get the same result: scrolling down worked fine the first time, but then scrolling up again messed up the cells background color.

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CartCell";
    CartCell *cell = (CartCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    Recipes *info = [_fetchedResultsController objectAtIndexPath:indexPath];

    if (cell == nil) 
    {
        cell = [[CartCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

//    if (!cell.nameLabel) {
//        cell.nameLabel = (UILabel*)[cell viewWithTag:1];
//        //        cell.nameLabel = (UILabel*)[cell viewWithTag:1];
//    }
//    if (!cell.ingredientsLabel)
//        cell.ingredientsLabel = (UILabel*)[cell viewWithTag:2];

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
    CGSize size = [info.ingredients sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    [cell.nameLabel setFrame:CGRectMake(10, 10, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), NAME_CELL_HEIGHT)];
    [cell.ingredientsLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN + NAME_CELL_HEIGHT, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];

    // SETTING TEXT CONTENT
    cell.nameLabel.text = info.name;
    cell.ingredientsLabel.text = info.ingredients;

    // SETTING BACKGROUND COLOR

    //        UIView *lab = [[UIView alloc] initWithFrame:cell.frame];
    //        [lab setBackgroundColor:[UIColor blueColor]];

    if (myStore.cellBackgroundShouldBeLight == YES) {
        NSLog(@"clear? %@", myStore.cellBackgroundShouldBeLight ? @"Yes" : @"No");
        cell.contentView.backgroundColor = [[UIColor alloc]initWithRed:87.0/255.0 green:84.0/255.0 blue:229.0/255.0 alpha:1];
        //            cell.backgroundView = lab;
        //            ingredientsLabel.backgroundColor = [UIColor clearColor];
        //            nameLabel.backgroundColor = [[UIColor alloc]initWithRed:87.0/255.0 green:168.0/255.0 blue:229.0/255.0 alpha:1];
        //            [cell setBackgroundColor: [[UIColor alloc]initWithRed:87.0/255.0 green:168.0/255.0 blue:229.0/255.0 alpha:1]];
        //            [cell setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
        myStore.cellBackgroundShouldBeLight = NO;
    } else {
//        cell.contentView.tag = 2;
        NSLog(@"clear? %@", myStore.cellBackgroundShouldBeLight ? @"Yes" : @"No");
        cell.contentView.backgroundColor = [[UIColor alloc]initWithRed:187.0/255.0 green:184.0/255.0 blue:229.0/255.0 alpha:1];
        myStore.cellBackgroundShouldBeLight = YES;
    }


    return cell;
}


推荐答案

indexPath告诉你你需要知道的一切。如果indexPath.row是偶数,那么使用一种颜色。如果indexPath.row是奇数,则使用不同的颜色。

It is very simple, the indexPath tells you everything you need to know. If the indexPath.row is even then use one color. If the indexPath.row is odd use a different color.

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    …

    // SETTING BACKGROUND COLOR

    //        UIView *lab = [[UIView alloc] initWithFrame:cell.frame];
    //        [lab setBackgroundColor:[UIColor blueColor]];

    if (indexPath.row % 2) {
        cell.contentView.backgroundColor = [[[UIColor alloc]initWithRed:87.0/255.0 green:84.0/255.0 blue:229.0/255.0 alpha:1] autorelease];
    } else {
        cell.contentView.backgroundColor = [[[UIColor alloc]initWithRed:187.0/255.0 green:184.0/255.0 blue:229.0/255.0 alpha:1] autorelease];
    }

    …

    return cell;
}






因为盲目假设细胞将被要求在交替对是一个坏的假设。 tableView可以请求以任何顺序选择单元格。在你的例子中,我相信细胞可能被要求如下。首先,要求0,1,...,9。接下来,向下滚动,并获取10,11和12。此时,0,1和2已经离开屏幕。你向上滚动回来,2被要求,但是不,你的模型是一个奇数交替,所以你得到错误的颜色。


Your method is having problems because blindly assuming cells will be asked for in alternating pairs is a bad assumption. The tableView could ask for cells in any order is chooses. In your example, I believe cells could be asked for as follows. First, 0, 1,…, 9 are asked for. Next, you scroll down and 10, 11, and 12 are fetched. At this point, 0, 1, and 2 have gone off the screen. You scroll back up and 2 is asked for, but oh no, your model is on an odd number alternation, so you get the wrong color.

这篇关于UITableViewCell与自定义单元格中的备用背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆