Chris1994
Chris1994

Reputation: 93

Xcode: How to change text colour of one table view cell

I have a table view showing a date set via a date picker. I want to compare the date picker date to the current date and if date picker is older than the current date i want the date to show in orange. The code i currently have works but it changes the colour in ever cell not just the one that is older. Any help would be much appreciated.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDate *today = [NSDate date];
    NSDate *Storeddate = [[NSUserDefaults standardUserDefaults] objectForKey:@"Datepicker"];
    NSComparisonResult result;

    result = [today compare:Storeddate];

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    NSManagedObject *Data = [ArrayData objectAtIndex:indexPath.row];
    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [Data valueForKey:@"dataattribute"]]];
    [cell.detailTextLabel setText:[NSString stringWithFormat:@"%@", [Data valueForKey:@"date"]]];

    if (result == NSOrderedDescending) {

        cell.detailTextLabel.textColor = [UIColor orangeColor];
    }
    else{
        cell.detailTextLabel.textColor = [UIColor blackColor];
    }

    return cell;
}

Upvotes: 0

Views: 276

Answers (1)

rebello95
rebello95

Reputation: 8576

Your result is comparing the same two dates every time. You are getting the two values like this:

NSDate *today = [NSDate date];
NSDate *Storeddate = [[NSUserDefaults standardUserDefaults] objectForKey:@"Datepicker"];

Both of those are constant values (unless you change the Datepicker object at some point inbetween loading each cell).

I'm assuming you want to compare the [Data valueForKey:@"date"] to today instead.

Upvotes: 1

Related Questions