开发者

Having trouble overriding uitableviewcell properties

开发者 https://www.devze.com 2023-03-05 19:35 出处:网络
I have a custom TableViewCell that I\'ve created. Each cell has two UIlabels that I want to be different colors. Also, I have one section of my tableView that I want to have a different background col

I have a custom TableViewCell that I've created. Each cell has two UIlabels that I want to be different colors. Also, I have one section of my tableView that I want to have a different background color and text color.

Right now, I can't get the text color to change using my UILabel.textColor; although I did get it to work with cell.textLabel.textColor. Also cell.background color isn't working.

I was able to get the cell property changes to work if I did it inside my switch statement at the bottom, but the cell properties wouldn't deque properly so as I scrolled up and down random cells would become white text on red background.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath
{
    static NSString *CellIdentifier = @"MyIdentifier";

    UILabel* itemLabel;
    UILabel* amountLabel;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    itemLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 35.0)] autorelease];
    itemLabel.tag = ITEMLABEL_TAG;
    itemLabel.font = [UIFont systemF开发者_开发技巧ontOfSize:14.0];
    itemLabel.textAlignment = UITextAlignmentLeft;
    itemLabel.autoresizingMask = UIViewAutoresizingNone;
    itemLabel.backgroundColor = [UIColor clearColor];
    itemLabel.textColor = [UIColor blueColor]; //This doesn't work

    if (indexPath.section == 4)
    {
        cell.textLabel.textColor = [UIColor whiteColor]; //This works
        cell.backgroundColor = [UIColor redColor]; //This doesn't work
    }
    [cell.contentView addSubview:itemLabel];

    cell.backgroundColor = [UIColor whiteColor];

    amountLabel = [[[UILabel alloc]initWithFrame:CGRectMake(225.0, 0.0, 55.0, 35.0)] autorelease];
    amountLabel.tag = AMOUNTLABEL_TAG;
    amountLabel.font = [UIFont systemFontOfSize:14.0];
    amountLabel.textAlignment = UITextAlignmentLeft;
    amountLabel.textColor = [UIColor blackColor]; //This doesn't work
    amountLabel.backgroundColor = [UIColor clearColor];
    amountLabel.autoresizingMask = UIViewAutoresizingNone;
    [cell.contentView addSubview:amountLabel];


    switch (indexPath.section) {
        case 0:
            cell.textLabel.text = [monthlyExpenses objectAtIndex:indexPath.row];
            break;
        case 1:
            cell.textLabel.text = [oneTimeFees objectAtIndex:indexPath.row];
            break;
        case 2:
            cell.textLabel.text = [renters objectAtIndex:indexPath.row];
            break;
        case 3:
            cell.textLabel.text = [travelExpenses objectAtIndex:indexPath.row];
            break;
        case 4:
            cell.textLabel.text = @"Generate Report";
            break;
    }

return cell;
}


You will need to change the background color of the cell in a - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

This is where you can do customization just before the table displays the cell.

0

精彩评论

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