2013-12-14 42 views
0

你好,對不起,我是個初學者。我有UILabel中的表格行值與核心數據加載,我需要以某種方式比較和找到最大的價值,從而將背景顏色更改爲紅色UILabel。每個答案我都很高興。從項目中獲得價值UitableView

- (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

你想知道什麼最大價值? –

+0

這是UILabel中標記爲20的浮點數的數字值 – Teoo333

+0

可以詳細說明嗎? –

回答

1

與獲取最大價值:

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

然後在cellForRowAtIndexPath可以比較這個數字與當前行的數量來決定做什麼。

+0

返回由於未捕獲的異常「NSUnknownKeyException」而終止應用程序,原因:'[ valueForUndefinedKey:]:實體Voda不是密鑰值編碼兼容鍵「@max」。 「 – Teoo333

+0

Voda ist實體核心數據 – Teoo333

+0

非常感謝您的工作,現在如何比較和更改UILabel背景顏色的最大價值。 – Teoo333