我的应用程序动态添加表格视图行,这些行接收显示为单元格字符串的通过/失败结果(除了描述之外)。我试图计算文本中收到失败结果的单元格数量。我的问题是我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;
}