0

私のアプリは、(説明に加えて) セル文字列として表示される合格/不合格の結果を受け取るテーブル ビュー行を動的に追加します。テキストで失敗した結果を受け取るセルの量を数えようとしています。私の問題は、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;
 }
4

2 に答える 2

2

ループ内でカウンターを初期化しているようで、常にリセットされます。ループの前に変数を宣言し、ループ内で単純に繰り返します。

于 2013-05-18T18:49:38.740 に答える
1

あなたが持っている:

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

これが 1 以外の値を返すと予想するのはなぜですか?

いくつかの条件を満たす行の数を数えたい場合は、データ (セルではなく) をループし、データの各値を確認する必要があります。

于 2013-05-18T18:49:55.297 に答える