线程1:Exc_Bad_Instruction(代码= Exc_1386_INVOP,子代码= 0x0),同时填充tableview

时间:2017-03-25 20:57:03

标签: ios swift uitableview

我制作的应用程序会从公开的Google表格中删除数据,而我现在正尝试将这些数据细分为不同的表格。我已经加载了数据,但现在当我尝试细分数据时,我收到一条错误消息:

  

Thread1:Exc_Bad_Instruction(Code = Exc + 1386_INVOP,subcode = 0x0)

在代码将值分配给表的开头标记。我是Swift的新手,但我知道如何用其他语言解决这个问题。

以下是代码:

import UIKit
import Kanna
import Alamofire

class AnnouncementViewController: UIViewController,     UITableViewDelegate, UITableViewDataSource {
    var items: [String] = []
    let textCellIdentifier = "Cell"

    override func viewDidLoad() {
        super.viewDidLoad()
        self.scrapeMorningReport()

        morningReportTableView.estimatedRowHeight = 140
        morningReportTableView.rowHeight = UITableViewAutomaticDimension
    }

    // Grabs the HTML from nycmetalscene.com for parsing.
    func scrapeMorningReport() -> Void {
        Alamofire.request("https://docs.google.com/spreadsheets/d/stuff/pubhtml").responseString { response in
            print("\(response.result.isSuccess)")
            if let html = response.result.value {
                self.parseHTML(html: html)
            }
        }
    }

    func parseHTML(html: String) -> Void {
        if let doc = Kanna.HTML(html: html, encoding: String.Encoding.utf8) {
            // Search for nodes by CSS selector
            for item in doc.css("td[dir^='ltr']") {
                // Strip the string of surrounding whitespace.
                let itemString = item.text!.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)

                /*let regex = try! NSRegularExpression(pattern: "^!=()", options: [.caseInsensitive])

                if regex.firstMatch(in: itemString, options: [], range: NSMakeRange(0, itemString.characters.count)) != nil {*/
                    items.append(itemString)
                    print("\(itemString)\n")
            }
        }

        self.filterItems()
        self.morningReportTableView.reloadData()
    }

    @IBOutlet weak var morningReportTableView: UITableView!

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        //return items.count
        return 30
    }

    func filterItems(){
        for i in 0 ..< items.count{
            if(items[i] == "None"){
                items[i] = ""
            }
        }
        items = items.flatMap{$0}
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: textCellIdentifier, for: indexPath) 

        let row = indexPath.row

        switch (row){
        case 0, 1, 2:
            cell.textLabel?.text = items[(indexPath.row + 15)]
        case 3, 4, 5:
            cell.textLabel?.text = items[(indexPath.row + 18)]
        case 6, 7, 8:
            cell.textLabel?.text = items[(indexPath.row + 21)]
        case 9, 10, 11:
            cell.textLabel?.text = items[(indexPath.row + 24)]
        case 12, 13, 14:
            cell.textLabel?.text = items[(indexPath.row + 27)]
        case 15, 16, 17:
            cell.textLabel?.text = items[(indexPath.row + 30)]
        case 18, 19, 20:
            cell.textLabel?.text = items[(indexPath.row + 33)]
        case 21, 22, 23:
            cell.textLabel?.text = items[(indexPath.row + 36)]
        case 24, 25, 26:
            cell.textLabel?.text = items[(indexPath.row + 39)]
        case 27, 28, 29:
            cell.textLabel?.text = items[(indexPath.row + 42)]
        default:
            print("Ooops")
        }

        return cell
    }
}

任何帮助都非常感谢!

0 个答案:

没有答案