gpt4 book ai didi

ios - 从 UitableView 项目中获取值

转载 作者:行者123 更新时间:2023-11-29 03:24:53 24 4
gpt4 key购买 nike

您好,很抱歉我是初学者。我在 UILabel 中有一个表行值加载了核心数据,我需要以某种方式比较并找到最大值,从而将背景颜色更改为红色 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];
}
}

最佳答案

获取最大值:

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

然后在 cellForRowAtIndexPath 中,您可以将此数字与当前行的数字进行比较,以确定要做什么。

关于ios - 从 UitableView 项目中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20580889/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com