小但可能是愚蠢的错误。我正在从文档目录中的文件中读取。我根据特定键的值过滤结果。并将具有该特定值内的特定字符串的任何内容呈现给UITableView。行数已正确填充,但是cell.textLabel.text没有在每个单元格rowForAtIndexPath上显示正确的名称。我的问题是,我是否正确遍历了循环?我是否由于indexPath设置了不正确的textLabel?我已经附上了输出的屏幕截图和一个NSLog语句,该语句显示了该特定对象的假定indexPath。这是显示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
这是屏幕截图。
最佳答案
从您的代码中删除for循环。
因为它将始终在您的datasourceArray中分配最后一个值
和使用
[tableDataSourceArray objectAtIndex:indexPath.row]
代替
[tableDataSourceArray objectAtIndex:objectToDisplay]
关于ios - 来自for循环的最后一条记录在每个单元格forRowAtIndexPath中重复,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39104843/