您好我很抱歉我是初学者。我在UILabel中有一个表行值,它们加载了Core Data,我需要以某种方式比较并找到最大值,从而将背景颜色更改为红色UILabel。我对每个答案都很满意。
- (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];
}
}
答案 0 :(得分:1)
获取最大值:
NSNumber *maxSpotreba = [self.voda valueForKeyPath:@"@max.spotreba"];
然后在cellForRowAtIndexPath
中您可以将此数字与当前行的数字进行比较,以确定要执行的操作。