2015-12-30 97 views
1

我想要的结果,我从一个API请求,这看起来是这样的(数组的数组)插入表中获取转换:显示信息数组的数组从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

+0

可你清楚你正试图转换是什么? –

+0

@ O-mkar对不起,我错误地打错了 – Abugado

+0

你遇到的问题是什么?你只是不知道该怎么做?还是有一个具体问题? – Idris

回答

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

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