将API中的数组数组显示为表格

时间:2015-12-30 05:12:36

标签: ios arrays swift

我正在尝试将我从api请求获得的结果转换为表格中的这个(数组数组):

请求结果:

[(
    "LEC 001",
    "10:00",
    "11:20",
    PHY,
    150,
    "Tompkins,Dave"
), (
    "LEC 002",
    "10:00",
    "11:20",
    PHY,
    235,
    "Holtby,Daniel James"
), (
    "LEC 003",
    "11:30",
    "12:50",
    MC,
    4045,
    "Akinyemi,John Akinlabi"
), (
    "LEC 004",
    "11:30",
    "12:50",
    MC,
    2038,
    "Roegiest,Adam Micheal"
), (
    "LEC 005",
    "13:00",
    "14:20",
    RCH,
    307,
    "Tompkins,Dave"
), (
    "LEC 006",
    "13:00",
    "14:20",
    DWE,
    3522,
    "Istead,Lesley Ann"
), (
    "LEC 007",
    "14:30",
    "15:50",
    DWE,
    3522,
    "Istead,Lesley Ann"
), (
    "LEC 008",
    "14:30",
    "15:50",
    MC,
    1056,
    "Holtby,Daniel James"
), (
    "LEC 009",
    "16:00",
    "17:20",
    MC,
    2034,
    "Akinyemi,John Akinlabi"
), (
    "LEC 010",
    "16:00",
    "17:20",
    MC,
    2035,
    "Roegiest,Adam Micheal"
), (
    "LEC 011",
    "08:30",
    "09:50",
    MC,
    4020,
    "Heinle,Albert"
)]

这就是我想象表格的样子

Image description

2 个答案:

答案 0 :(得分:0)

  

1)首先我解析了你在元组中的数据

如果您想了解更多如何从元组中获取价值,可以参考此文章 http://www.codingexplorer.com/tuples-in-swift-create-read-and-return/

这是输出

PlayGround output

  

2)我在表格视图中实现了

//
//  ViewController.swift
//  test tableview
//
//  Created by O-mkar on 30/12/15.
//  Copyright © 2015 test. All rights reserved.
//

import UIKit

class ViewController: UITableViewController {

    let testArray = [(
        "LEC 001",
        "10:00",
        "11:20",
        "PHY,150",
        "Tompkins,Dave"
        ), (
            "LEC 002",
            "10:00",
            "11:20",
            "PHY,235",
            "Holtby,Daniel James"
        ), (
            "LEC 003",
            "11:30",
            "12:50",
            "MC,4045",
            "Akinyemi,John Akinlabi"
        ), (
            "LEC 004",
            "11:30",
            "12:50",
            "MC,2038",
            "Roegiest,Adam Micheal"
        ), (
            "LEC 005",
            "13:00",
            "14:20",
            "RCH,307",
            "Tompkins,Dave"
        ), (
            "LEC 006",
            "13:00",
            "14:20",
            "DWE,3522",
            "Istead,Lesley Ann"
        ), (
            "LEC 007",
            "14:30",
            "15:50",
            "DWE,3522",
            "Istead,Lesley Ann"
        ), (
            "LEC 008",
            "14:30",
            "15:50",
            "MC,1056",
            "Holtby,Daniel James"
        ), (
            "LEC 009",
            "16:00",
            "17:20",
            "MC,2034",
            "Akinyemi,John Akinlabi"
        ), (
            "LEC 010",
            "16:00",
            "17:20",
            "MC,2035",
            "Roegiest,Adam Micheal"
        ), (
            "LEC 011",
            "08:30",
            "09:50",
            "MC,4020",
            "Heinle,Albert"
        )]

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return testArray.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let myCell:UITableViewCell=tableView.dequeueReusableCellWithIdentifier("prototype", forIndexPath: indexPath)

        myCell.textLabel?.text = "\(testArray[indexPath.row].0) | \(testArray[indexPath.row].1) | \(testArray[indexPath.row].2) | \(testArray[indexPath.row].3) | \(testArray[indexPath.row].4) "

        return myCell

        //END ADDING ICONS
    }
    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?{

        return "SEC | Starts | Ends | Building | Inst "

    }



}
  

输出

OUTPUT

以下是我可以使用它的项目链接,并对其进行测试并自定义您想要的方式 https://drive.google.com/file/d/0B2csGr9uKp1DMWRhSjlCTWdFUEU/view?usp=sharing

答案 1 :(得分:0)

只有一种方法:

在Swift中,为数组定义了flatten方法。 flatten"变平"数组的数组:

let aa = [[1,2,3], [4,5,6], [7,8,9]]

aa.flatten().forEach {
    print($0)
}

打印:

1
2
3
4
5
6
7
8
9