Teoo333
Teoo333

Reputation: 1

Get value from item UitableView

Hello I'm sorry I'm a beginner. I have a table row values ​​in UILabel that are loaded with Core Data and I need to somehow compare and find the greatest value and thus change the background color to red UILabel. I'm very happy for each answer.

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
   {
     // Return the number of sections.
        return 1;
   }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
   {
     // Return the number of rows in the section.
        return self.voda.count;
    }

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        // Configure the cell...
         NSManagedObject *vodapocet = [self.voda objectAtIndex:indexPath.row];

         UILabel *dLabel= (UILabel *)[cell viewWithTag:10];
         [dLabel setText:[NSString stringWithFormat:@"%@" ,[vodapocet valueForKey:@"datum"]]];

         UILabel *sLabel= (UILabel *)[cell viewWithTag:20];
         [sLabel setText:[NSString stringWithFormat:@"%@" ,[vodapocet valueForKey:@"spotreba" ]]];
         [sLabel setBackgroundColor:[UIColor greenColor]];

         UILabel *cLabel= (UILabel *)[cell viewWithTag:30];
         [cLabel setText:[NSString stringWithFormat:@"%@" ,[vodapocet valueForKey:@"cena"]]];

          return cell;
  }

 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
 }

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSManagedObjectContext *context = [self managedObjectContext];

  if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete object from database
    [context deleteObject:[self.voda objectAtIndex:indexPath.row]];

    NSError *error = nil;
    if (![context save:&error]) {
        NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
        return;
    }

        // Remove device from table view
    [self.voda removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]   withRowAnimation:UITableViewRowAnimationFade];
  }
 }

Upvotes: 0

Views: 112

Answers (1)

Wain
Wain

Reputation: 119031

Get the maximum value with:

NSNumber *maxSpotreba = [self.voda valueForKeyPath:@"@max.spotreba"];

Then in cellForRowAtIndexPath You can compare this number with the number for the current row to determine what to do.

Upvotes: 1

Related Questions