2016-08-23 7 views
0

ここでは小規模ですが、おそらく愚かな間違いです。私はドキュメントディレクトリからファイルを読み込んでいます。特定のキーの値に基づいて結果をフィルタリングします。そして、特定の値を持つ特定の文字列を持つものは、私がUITableViewに提示します。行の数は正しく入力されますが、cell.textLabel.textは各セルのrowForAtIndexPathに正しい名前を表示していません。私の質問は、ループを正しく繰り返していますか? indexPathのためにtextLabelを間違って設定していますか?出力のスクリーンショットと、特定のオブジェクトの想定されるindexPathを示すNSLogステートメントを添付しました。 willDisplayCell forRowAtIndexPathを示すコードスニペットを次に示します。forループからの最後のレコードは、各セルforRowAtIndexPathで複製されます。

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
if (indexPath.row%2 == 0) { 
    UIColor *altCellColor = [UIColor colorWithWhite:0.7 alpha:0.2]; 
    cell.backgroundColor = altCellColor; 
    } 

for (int objectToDisplay = 0; objectToDisplay<[tableDataSourceArray count]; objectToDisplay++) { 
    NSLog(@"%lu", (unsigned long)objectToDisplay); 
    NSString *objectTitle = [[tableDataSourceArray objectAtIndex:objectToDisplay] valueForKey:@"ConnectabilityObjectTitle"]; 
    NSString *pingResult = [[tableDataSourceArray objectAtIndex:objectToDisplay] valueForKey:@"ConnectivityPingTestResultValue"]; 
    NSString *portConnectResult = [[tableDataSourceArray objectAtIndex:objectToDisplay] valueForKey:@"ConnectivityPortConnectResultValue"]; 

    if ([pingResult containsString:@"Successful"]) { 
     if ([portConnectResult isEqualToString:@"Successful"]) { 
     } else { 
      cell.textLabel.text = objectTitle; 

      cell.detailTextLabel.text = @"Failed to connect to port for this server/service"; 
      NSLog(@"%@ at row %lu", objectTitle, (unsigned long)objectToDisplay); 
     } 
    } else { 
     cell.textLabel.text = objectTitle; 
     cell.detailTextLabel.text = @"Failed to ping this server/service"; 
    } 
    } 
} 

ここにコンソールからのNSLogStatementがあります。

2016-08-23 11:04:11.509 FlightPath[1346:551416] 0 
2016-08-23 11:04:11.510 FlightPath[1346:551416] 1 
2016-08-23 11:04:11.510 FlightPath[1346:551416] 2 
2016-08-23 11:04:11.510 FlightPath[1346:551416] 3 
2016-08-23 11:04:11.510 FlightPath[1346:551416] 4 
2016-08-23 11:04:11.511 FlightPath[1346:551416] 5 
2016-08-23 11:04:11.513 FlightPath[1346:551416] Store Drive at row 5 
2016-08-23 11:04:11.513 FlightPath[1346:551416] 6 
2016-08-23 11:04:11.514 FlightPath[1346:551416] 7 
2016-08-23 11:04:11.514 FlightPath[1346:551416] 8 
2016-08-23 11:04:11.514 FlightPath[1346:551416] 9 
2016-08-23 11:04:11.515 FlightPath[1346:551416] 10 
2016-08-23 11:04:11.515 FlightPath[1346:551416] 11 
2016-08-23 11:04:11.515 FlightPath[1346:551416] 12 
2016-08-23 11:04:11.517 FlightPath[1346:551416] 13 
2016-08-23 11:04:11.517 FlightPath[1346:551416] 14 
2016-08-23 11:04:11.517 FlightPath[1346:551416] 15 
2016-08-23 11:04:11.518 FlightPath[1346:551416] 16 
2016-08-23 11:04:11.518 FlightPath[1346:551416] 17 
2016-08-23 11:04:11.518 FlightPath[1346:551416] 18 
2016-08-23 11:04:11.518 FlightPath[1346:551416] 19 
2016-08-23 11:04:11.519 FlightPath[1346:551416] 20 
2016-08-23 11:04:11.519 FlightPath[1346:551416] 21 
2016-08-23 11:04:11.519 FlightPath[1346:551416] 22 
2016-08-23 11:04:11.522 FlightPath[1346:551416] 23 
2016-08-23 11:04:11.522 FlightPath[1346:551416] 24 
2016-08-23 11:04:11.522 FlightPath[1346:551416] StorewebP at row 24 
2016-08-23 11:04:11.523 FlightPath[1346:551416] 25 
2016-08-23 11:04:11.523 FlightPath[1346:551416] 26 
2016-08-23 11:04:11.523 FlightPath[1346:551416] FIM DataCenter VIP at row 26 
2016-08-23 11:04:11.524 FlightPath[1346:551416] 27 

ここにスクリーンショットがあります。 TableViewDuplicateOfLastRecordIssue

+0

を使用するので? 'objectAtIndex:objectToDisplay'の代わりに' objectAtIndex:indexPath.row'を使うのではないでしょうか? –

+0

forループは基本的に、DocumentsDirectory内のファイルから派生した配列をループしています。 この配列を反復処理しているので、forループが完全に必要であると思いますか?私は各レコードをループして、セルを対応する情報で表示するために値をチェックしていることに注意してください。 –

+0

私はあなたが各セルにつき1つのレコードを持っていると仮定しました。それが間違っている場合、ファイルはどのように細胞に関連していますか? –

答えて

1

コードからforループを削除します。
それは常にあなたのdatasourceArray に最後の値を割り当て、

[tableDataSourceArray objectAtIndex:indexPath.row] 

の代わりに、あなたはforループを持っていないのはなぜ

[tableDataSourceArray objectAtIndex:objectToDisplay] 
+0

次は私の次の質問です。 forループを削除すると言うと、私のforループの背後にある根本的な理由は、基本的に各レコードを繰り返し処理し、特定のキーに対して「失敗」した値をチェックすることです。したがって、2つの特定のキーに失敗した値がある場合、それらの対応するキーと値を含むNSMutableDictonaryになる特定のオブジェクト...でTableViewを設定する必要があります。 –

関連する問題