ios - 将来自 API 的数组的数组信息显示为表格

标签 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

最佳答案

1) First i parsed your data it was in tuple

如果你想了解更多如何从元组中获取值,你可以引用这篇文章 http://www.codingexplorer.com/tuples-in-swift-create-read-and-return/

这是输出

PlayGround output

2) i implemented in the table view

//
//  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

OUTPUT

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

关于ios - 将来自 API 的数组的数组信息显示为表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34523288/

相关文章:

javascript - 在 forEach 数组中使用 for 循环

php - 检查一个数组是否可以放入另一个数组

ios - 如何跳转到UIPageViewController中的特定页面,setViewControllers不起作用

swift - 如何从 firebase 下载多个图像?

ios - UITableView 中来自 NSData 的 UIImage - 性能问题

ios - 比较包含 NSStrings 的 NSDictionaries

ios - 调用一个 swift 方法从 objective-c 执行 segue

PHP - 使用 array_filter 从哈希表(数组)中删除项目

ios - Swift UIView 背景颜色不透明度

ios - UIWebView 在下拉时崩溃