来自for循环的最后一条记录在每个Cell forRowAtIndexPath

时间:2016-08-23 15:10:02

标签: ios objective-c uitableview

小但可能是愚蠢的错误。我正在从文档目录中读取文件。我根据特定键的值过滤结果。任何具有特定值的特定字符串的任何内容都会呈现在UITableView中。正确填充了行数,但是cell.textLabel.text并未在每个单元格rowForAtIndexPath中显示正确的名称。我的问题是,我正在循环我的循环吗?我是否因为indexPath而错误地设置textLabel?我附上了输出的屏幕截图和一个显示该特定对象的假定indexPath的NSLog语句。这是一段显示willDisplayCell 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

1 个答案:

答案 0 :(得分:1)

从您的代码中删除for循环 因为它总是会在datasourceArray中分配最后一个值 并使用

[tableDataSourceArray objectAtIndex:indexPath.row]

而不是

[tableDataSourceArray objectAtIndex:objectToDisplay]