开发者

UITableView, color specific cell's text by using indexpath.row == ..., Colors more than one cell

开发者 https://www.devze.com 2023-04-03 14:20 出处:网络
I can\'t figure out whats happening here and why. I identify a specific index that I want to target to change the color on. And inside:

I can't figure out whats happening here and why. I identify a specific index that I want to target to change the color on. And inside:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSI开发者_如何学GondexPath *)indexPath{

I run the following:

if ([savedPRIndex intValue] == indexPath.row) {
        [customCell togglePR:TRUE];
        NSLog(@"TRUE");
    }else{
        [customCell togglePR:FALSE];
    }

In custom Cell

- (void)layoutSubviews{

    [super layoutSubviews];
    CGFloat xPosition = 20.0f; // Default text position

    if(prToggle){
        xPosition = -20.0f;
        cellText.textColor = [UIColor colorWithRed:0x24/255.0 green:0x9e/255.0 blue:0xd6/255.0 alpha:1.0];       
        UIImage* img = [UIImage imageNamed:@"pr_icon.png"];
        CGRect rect = CGRectMake(272.0f, 14.0f, img.size.width/2, img.size.height/2);
        UIImageView* imgView = [[UIImageView alloc] initWithFrame:rect];
        [imgView setImage:img];

        [self addSubview:imgView];
        [imgView release];
    }

    CGRect textLabelFrame = self.cellText.frame;
    textLabelFrame.origin.x = xPosition;
    self.cellText.frame = textLabelFrame;
}

-(void)togglePR:(BOOL)TF{
    prToggle = TF;
}

custom cell togglePR is the only place where the text can change color, anyone have any ideas?

I can post more code if it would help decipher whats going on.

Additional Code

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

    static NSString *CellIdentifier = @"Cell";

    CustomCell *customCell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (customCell == nil) {
        customCell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    NSLog(@"savedPRIndex : %@", savedPRIndex);
    NSLog(@"Index Path : %i", indexPath.row);

    [customCell togglePR:[savedPRIndex intValue] == indexPath.row];

    // Configure the cell...
    customCell.cellText.text = [[toShow objectAtIndex:indexPath.row] objectForKey:@"Value"];
    customCell.cellPower.text = [[toShow objectAtIndex:indexPath.row] objectForKey:@"PowerValue"];
    customCell.cellSplit.text = [[toShow objectAtIndex:indexPath.row] objectForKey:@"Split"];

    return customCell;
}


Whereabouts is your second if statement? If it is when you are creating the cell initially this could lead to multiple cells being set as the cell is reused. Your cellForRowAtIndexPath should have the following structure:

  • dequeue cell
  • check if a cell object was returned, initialise a new one if not
  • configure the cell (either the dequeued cell or the newly created cell)

My guess is that your code above is in part 2 rather than part 3.

EDIT: just seen your updated code. Once you have set the toggle, you don't appear to have a mechanism to unset it - eg remove the subview created and so forth. You could be dequeuing a cell with the toggle set, but setting it to NO has no effect.


Chances are you did not show us enough code and have that code inside of the if(!cell) check when attempting to dequeue a cell. That means it will only be run for the initial creation of the cell and not for dequeued cells.

Now side notes, in Objective-C use YES and NO instead of TRUE and FALSE, and when you need to set a boolean, there is no need to use an if check because the boolean expression will work.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableview dequeue...:@"blah"];
    if(!cell)
    {
        //Create cell
        //Do not set the colors here
    }

    //Set the colors here (no need for an if check)
    [customCell togglePR:[savedPRIndex intValue] == indexPath.row];
}

Edit:

Based on your update you should call - (void)setNeedsLayout or - (void)setNeedsDisplay when you set the PR flag.

-(void)togglePR:(BOOL)TF{
    prToggle = TF;
    [self setNeedsDisplay];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消