具有多个 TableView 键的 NSArray

NSArray with several Keys to TableView

我正在开发一个天气应用程序。我获取了预测数据,预测的每一天都有一个 NSArray。该数组如下所示:

(
        {
        apparentTemperatureMax = "73.44";
        apparentTemperatureMaxTime = 1467500400;
        apparentTemperatureMin = "47.12";
        apparentTemperatureMinTime = 1467457200;
        cloudCover = "0.75";
        dewPoint = "43.53";
        humidity = "0.58";
        icon = rain;
        moonPhase = "0.9399999999999999";
        ozone = "276.53";
        precipIntensity = "0.0036";
        precipIntensityMax = "0.0092";
        precipIntensityMaxTime = 1467504000;
        precipProbability = "0.44";
        precipType = rain;
        pressure = "1015.28";
        summary = "Light rain starting in the afternoon.";
        sunriseTime = 1467460332;
        sunsetTime = 1467513207;
        temperatureMax = "73.44";
        temperatureMaxTime = 1467500400;
        temperatureMin = "47.12";
        temperatureMinTime = 1467457200;
        time = 1467439200;
        windBearing = 281;
        windSpeed = "0.54";
    }
)

我把所有的日子合为一个巨人NSMutableArray。我想做的是将 NSMutableArray 作为 UITableView 的来源,但只输入一些天气数据,如 temperatureMin、temperatureMax、precipProbability 等。对我来说,采用包含多个数组的 NSMutableArray 并且只显示每个数组的一些信息的最佳方式是什么?

更新:

这是完整的 NSMutableArray:

(
        (
                {
            apparentTemperatureMax = "73.44";
            apparentTemperatureMaxTime = 1467500400;
            apparentTemperatureMin = "47.12";
            apparentTemperatureMinTime = 1467457200;
            cloudCover = "0.75";
            dewPoint = "43.53";
            humidity = "0.58";
            icon = rain;
            moonPhase = "0.9399999999999999";
            ozone = "276.53";
            precipIntensity = "0.0036";
            precipIntensityMax = "0.0092";
            precipIntensityMaxTime = 1467504000;
            precipProbability = "0.44";
            precipType = rain;
            pressure = "1015.28";
            summary = "Light rain starting in the afternoon.";
            sunriseTime = 1467460332;
            sunsetTime = 1467513207;
            temperatureMax = "73.44";
            temperatureMaxTime = 1467500400;
            temperatureMin = "47.12";
            temperatureMinTime = 1467457200;
            time = 1467439200;
            windBearing = 281;
            windSpeed = "0.54";
        }
    ),
        (
                {
            apparentTemperatureMax = "69.59";
            apparentTemperatureMaxTime = 1467576000;
            apparentTemperatureMin = 56;
            apparentTemperatureMinTime = 1467540000;
            cloudCover = "0.41";
            cloudCoverError = "0.35";
            dewPoint = "45.21";
            dewPointError = "10.53";
            humidity = "0.55";
            humidityError = "0.19";
            icon = "partly-cloudy-day";
            moonPhase = "0.97";
            precipType = rain;
            pressure = "1016.93";
            pressureError = "1.78";
            summary = "Partly cloudy throughout the day.";
            sunriseTime = 1467546762;
            sunsetTime = 1467599598;
            temperatureMax = "69.59";
            temperatureMaxError = "9.68";
            temperatureMaxTime = 1467576000;
            temperatureMin = 56;
            temperatureMinTime = 1467540000;
            time = 1467525600;
            visibility = 10;
            visibilityError = "1.98";
            windBearing = 217;
            windBearingError = "38.17";
            windSpeed = "6.24";
            windSpeedError = "4.9";
        }
    ),
        (
                {
            apparentTemperatureMax = "69.69";
            apparentTemperatureMaxTime = 1467662400;
            apparentTemperatureMin = "55.94";
            apparentTemperatureMinTime = 1467622800;
            cloudCover = "0.39";
            cloudCoverError = "0.35";
            dewPoint = "45.61";
            dewPointError = "9.890000000000001";
            humidity = "0.55";
            humidityError = "0.19";
            icon = "partly-cloudy-day";
            moonPhase = "0.02";
            precipType = rain;
            pressure = "1017.46";
            pressureError = "1.74";
            summary = "Partly cloudy throughout the day.";
            sunriseTime = 1467633192;
            sunsetTime = 1467685988;
            temperatureMax = "69.69";
            temperatureMaxError = "9.69";
            temperatureMaxTime = 1467662400;
            temperatureMin = "55.94";
            temperatureMinError = "9.48";
            temperatureMinTime = 1467622800;
            time = 1467612000;
            visibility = 10;
            visibilityError = 2;
            windBearing = 216;
            windBearingError = "35.68";
            windSpeed = "6.72";
            windSpeedError = "4.83";
        }
    ),
        (
                {
            apparentTemperatureMax = "69.78";
            apparentTemperatureMaxTime = 1467748800;
            apparentTemperatureMin = "56.03";
            apparentTemperatureMinTime = 1467709200;
            cloudCover = "0.39";
            cloudCoverError = "0.35";
            dewPoint = "45.91";
            dewPointError = "9.67";
            humidity = "0.5600000000000001";
            humidityError = "0.19";
            icon = "partly-cloudy-day";
            moonPhase = "0.05";
            precipType = rain;
            pressure = "1017.36";
            pressureError = "1.7";
            summary = "Partly cloudy throughout the day.";
            sunriseTime = 1467719624;
            sunsetTime = 1467772376;
            temperatureMax = "69.78";
            temperatureMaxError = "9.65";
            temperatureMaxTime = 1467748800;
            temperatureMin = "56.03";
            temperatureMinError = "9.470000000000001";
            temperatureMinTime = 1467709200;
            time = 1467698400;
            visibility = 10;
            visibilityError = 2;
            windBearing = 215;
            windBearingError = "35.59";
            windSpeed = "6.7";
            windSpeedError = "4.8";
        }
    ),
        (
                {
            apparentTemperatureMax = "69.84999999999999";
            apparentTemperatureMaxTime = 1467835200;
            apparentTemperatureMin = "56.11";
            apparentTemperatureMinTime = 1467795600;
            cloudCover = "0.4";
            cloudCoverError = "0.35";
            dewPoint = "46.2";
            dewPointError = "9.470000000000001";
            humidity = "0.5600000000000001";
            humidityError = "0.19";
            icon = "partly-cloudy-day";
            moonPhase = "0.08";
            precipType = rain;
            pressure = "1017.26";
            pressureError = "1.67";
            summary = "Partly cloudy throughout the day.";
            sunriseTime = 1467806058;
            sunsetTime = 1467858761;
            temperatureMax = "69.84999999999999";
            temperatureMaxError = "9.57";
            temperatureMaxTime = 1467835200;
            temperatureMin = "56.11";
            temperatureMinError = "9.41";
            temperatureMinTime = 1467795600;
            time = 1467784800;
            visibility = 10;
            visibilityError = 2;
            windBearing = 214;
            windBearingError = "35.54";
            windSpeed = "6.67";
            windSpeedError = "4.77";
        }
    ),
        (
                {
            apparentTemperatureMax = "69.91";
            apparentTemperatureMaxTime = 1467921600;
            apparentTemperatureMin = "56.17";
            apparentTemperatureMinTime = 1467882000;
            cloudCover = "0.41";
            cloudCoverError = "0.35";
            dewPoint = "46.47";
            dewPointError = "9.32";
            humidity = "0.57";
            humidityError = "0.19";
            icon = "partly-cloudy-day";
            moonPhase = "0.12";
            precipType = rain;
            pressure = "1017.16";
            pressureError = "1.63";
            summary = "Partly cloudy throughout the day.";
            sunriseTime = 1467892492;
            sunsetTime = 1467945146;
            temperatureMax = "69.91";
            temperatureMaxError = "9.449999999999999";
            temperatureMaxTime = 1467921600;
            temperatureMin = "56.17";
            temperatureMinError = "9.300000000000001";
            temperatureMinTime = 1467882000;
            time = 1467871200;
            visibility = 10;
            visibilityError = 2;
            windBearing = 213;
            windBearingError = "35.55";
            windSpeed = "6.63";
            windSpeedError = "4.74";
        }
    ),
        (
                {
            apparentTemperatureMax = "69.95999999999999";
            apparentTemperatureMaxTime = 1468008000;
            apparentTemperatureMin = "56.23";
            apparentTemperatureMinTime = 1467968400;
            cloudCover = "0.41";
            cloudCoverError = "0.35";
            dewPoint = "46.71";
            dewPointError = "9.23";
            humidity = "0.57";
            humidityError = "0.19";
            icon = "partly-cloudy-day";
            moonPhase = "0.15";
            precipType = rain;
            pressure = "1017.08";
            pressureError = "1.6";
            summary = "Partly cloudy throughout the day.";
            sunriseTime = 1467978927;
            sunsetTime = 1468031528;
            temperatureMax = "69.95999999999999";
            temperatureMaxError = "9.31";
            temperatureMaxTime = 1468008000;
            temperatureMin = "56.23";
            temperatureMinError = "9.17";
            temperatureMinTime = 1467968400;
            time = 1467957600;
            visibility = 10;
            visibilityError = "2.01";
            windBearing = 212;
            windBearingError = "35.64";
            windSpeed = "6.57";
            windSpeedError = "4.71";
        }
    ),
        (
                {
            apparentTemperatureMax = "69.98999999999999";
            apparentTemperatureMaxTime = 1468094400;
            apparentTemperatureMin = "56.26";
            apparentTemperatureMinTime = 1468054800;
            cloudCover = "0.42";
            cloudCoverError = "0.35";
            dewPoint = "46.94";
            dewPointError = "9.18";
            humidity = "0.57";
            humidityError = "0.19";
            icon = "partly-cloudy-day";
            moonPhase = "0.18";
            precipType = rain;
            pressure = "1017.01";
            pressureError = "1.58";
            summary = "Partly cloudy throughout the day.";
            sunriseTime = 1468065364;
            sunsetTime = 1468117908;
            temperatureMax = "69.98999999999999";
            temperatureMaxError = "9.17";
            temperatureMaxTime = 1468094400;
            temperatureMin = "56.26";
            temperatureMinError = "9.029999999999999";
            temperatureMinTime = 1468054800;
            time = 1468044000;
            visibility = 10;
            visibilityError = "2.01";
            windBearing = 211;
            windBearingError = "35.84";
            windSpeed = "6.48";
            windSpeedError = "4.68";
        }
    )
)

table.

源码中数组有两种使用方式

1- 你可以从数组中获取值 - 前-

NSString * temperatureMax_Str = [NSString stringWithFormat:@"%@",[[Array objectAtIndex:indexPath.row] valueForKey:@"temperatureMax"]];

2- 创建一个 NSDictionary 并添加数组中的值 -

NSDictionary *Dic = [Array objectAtIndex:indexPath.row];
 NSString * temperatureMax_Str = [Dic  valueForKey:@"temperatureMax"];

还有其他一些方法,您也可以尝试创建 mutable 字典等

使用此单行代码为您的 cellForRowAtIndexpath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    sampleTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TableCellIdenfier"];
    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"TableCell" bundle:nil] forCellReuseIdentifier:@"TableCell"];
        cell=[tableView dequeueReusableCellWithIdentifier:@"TableCell"];
    }

    // Configure the cell...

    cell.temperatureLabel.text = [[[YourArrayName objectAtIndex:0] objectAtIndex:indexPath.row] objectForKey:@"temperatureMax"];

    return cell;
}

希望对您有所帮助

您应该创建一个模型对象,例如WeatherObject class 并将您的响应解析为此类对象的数组。丢弃您在解析期间想要丢弃的任何字段。在您可以将 WeatherObject 中的 NSMutableArray 设置为您的 table 视图的数据源之后。这将是更加面向对象和人类可读的代码。