gpt4 book ai didi

ios - 计算 TableView 中包含字符串的行数始终返回 1

转载 作者:行者123 更新时间:2023-11-28 20:19:04 26 4
gpt4 key购买 nike

我的应用程序动态添加 TableView 行,这些行接收显示为单元格字符串(除了描述)的通过/失败结果。我正在尝试计算文本中收到失败结果的单元格数量。我的问题是我的 NSLog(@"Counted times: %i", times); 总是返回 1 而不是将它们相加。

 cell.textLabel.text = [NSString stringWithFormat:@"Appliance %@:  %@", ((Circuit *)[self.circuits objectAtIndex:indexPath.row]).circuitReference,((Circuit *)[self.circuits objectAtIndex:indexPath.row]).rcdTestButton];
if(cell.textLabel.text && [cell.textLabel.text rangeOfString:@"Fail"].location != NSNotFound){
cell.textLabel.textColor = [UIColor redColor];

NSString *string = @"str";
int times = [[string componentsSeparatedByString:@"-"] count];

NSLog(@"Counted times: %i", times);

}

更新代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *string = @"str";
int times = [[string componentsSeparatedByString:@"str"] count];


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

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (indexPath.section == 0) {
cell.textLabel.text = @"Summary of appliance testing";
} else {
//Appliance label text
cell.textLabel.text = [NSString stringWithFormat:@"Appliance %@: %@", ((Circuit *)[self.circuits objectAtIndex:indexPath.row]).circuitReference,((Circuit *)[self.circuits objectAtIndex:indexPath.row]).rcdTestButton];
if(cell.textLabel.text && [cell.textLabel.text rangeOfString:@"Fail"].location != NSNotFound){
cell.textLabel.textColor = [UIColor redColor];

NSLog(@"Counted times: %i", times);

}

cell.imageView.image = [UIImage imageNamed:@""];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;
}

最佳答案

看起来您正在循环内初始化计数器,循环不断地重置它。在循环之前声明变量,然后在循环内简单地迭代它。

关于ios - 计算 TableView 中包含字符串的行数始终返回 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16627754/

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