斯威夫特:tableView [英] Swift: tableView

查看:28
本文介绍了斯威夫特:tableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了以下 tableView:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {let cell = tableView.dequeueReusableCellWithIdentifier("ProductCell", forIndexPath:indexPath)打印(将它传递给tableview \(courseName2)")打印(将它传递给tableview \(codeName2)")变量计数 = 0getTime( (self.courseName2!), courseCode: (self.codeName2!), completion:{(success:[ClassSchedule]) -> Void in计数 = 类 [0].total!打印(总CKASSESSSs \(计数))dispatch_async(dispatch_get_main_queue(),{self.tableView.reloadData()对于 var i = 0;我<数数;++我{//print("FOR 循环次数:\(i)")cell.textLabel?.text = "\(classes[i].section!)"//开始时间:\(classes[i].start!)结束时间:\(classes[i].end!)打印(循环计数:\(i)节\(类[i].节!)")}});})返回单元格}

for 循环函数中的打印语句提供了来自 'classes' 数组的正确信息,但是当我模拟代码时,只有最后一个元素填满了每一行.

我希望它按顺序显示LEC 001"、LEC 002"、LEC 003"、LEC 004"、TST 101",而不是TST 101",它们包含在classes"数组中.

getTime 函数,通过 JSON 调用获取数据:

//读取 JSON做 {让数据:NSData?= NSData(contentsOfURL: url)if let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as?NSD字典{//////大批///////让 dataArray = jsonResult["data"] 作为!数组//////让 arrayLength = dataArray.count变量计数 = 0//classes.append(ClassSchedule(course: item["subject"] as!String, code: item["catalog_number"] as!String, section: item["section"] as!String) )打印(总类数:\(数组长度)");对于 dataArray 中的项目{classes.append(ClassSchedule(course: "TEMP", code: "TEMP", section: "TEMP", days:"TEMP", start:"TEMP", end:"TEMP", building:"TEMP", room:TEMP",总计:arrayLength) )classes[count].course = (item["subject"] as!String)classes[count].code = (item["catalog_number"] as!String)classes[count].section = (item["section"] as!String)print("主题:\(classes[count].course!) \(classes[count].code!)");打印(部分:\(类[计数].部分!)");//print("Section: \(section_numb)");让 subjectArray = item["classes"] 作为!数组对于 subjectArray 中的 item2{让 dateDictionary = item2["date"] 作为!NSD字典//让 startTime = dateDictionary["start_time"]!//self.performSelectorOnMainThread("updateIPLabel:", withObject: startTime, waitUntilDone: false)//let endTime = dateDictionary["end_time"]!classes[count].days = (dateDictionary["weekdays"] as!String)classes[count].start = (dateDictionary["start_time"] as!String)classes[count].end = (dateDictionary["end_time"] as!String)string1 = classes[count].start!print("工作日:\(classes[count].days!)");print("开始时间:\(classes[count].start!)");print("结束时间:\(class[count].end!)");让 locationDictionary = item2["location"] as!NSD字典classes[count].building = (locationDictionary["building"] as? String)classes[count].room = (locationDictionary["room"] as?String)print("建筑:\(classes[count].building) \(classes[count].room)");计数 += 1打印(计数)打印("")}}//让主题 = dataArray["主题"]}} 抓住 {打印(坏事发生了")}//print("ASDIAWDWD: \(classes[0].section)")//打印("ASDIAWDWASDASDD: \(classes[1].section)")完成(类:类)}).恢复()

解决方案

很难确切地说出您想要做什么,但是您不应该在表视图数据源方法中使用任何异步方法.这些方法应该只代表您已经拥有的数据.好像你可以用 cell.textLabel?.text = "\(classes[indexPath.row].section!)" 替换整个 getTime 块.>

I have following tableView defined:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("ProductCell", forIndexPath:indexPath)
    print("Pass it to tableview \(courseName2) ")
     print("Pass it to tableview \(codeName2) ")
    var count = 0


    getTime( (self.courseName2!), courseCode: (self.codeName2!), completion:{(success:[ClassSchedule]) -> Void in
            count = classes[0].total!
            print("TOTAL CKASSESSSs \(count) ")
            dispatch_async(dispatch_get_main_queue(),{
                self.tableView.reloadData()
                for var i = 0; i < count; ++i
                {
                    //print("FOR LOOP COUNT: \(i)")
                    cell.textLabel?.text = "\(classes[i].section!)"
                    // Start Time: \(classes[i].start!)End Time: \(classes[i].end!)
                    print("FOR LOOP COUNT: \(i) Section\(classes[i].section!)")
                }

            });

    })
    return cell
}

The print statement in the for loop function provides correct information from the 'classes' array, but when I simulate the code, only the last element fills up every rows.

Instead of 'TST 101', I want it to display 'LEC 001', 'LEC 002', 'LEC 003', 'LEC 004', 'TST 101' in order, which are contained in 'classes' array.

getTime function, getting data through JSON call:

 // Read the JSON
    do {
        let data: NSData? = NSData(contentsOfURL: url)
        if let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
        {
            //////ARRAY///////
            let dataArray = jsonResult["data"] as! NSArray
            //////
            let arrayLength = dataArray.count
            var count = 0
            //classes.append(ClassSchedule(course: item["subject"] as! String, code: item["catalog_number"] as! String, section: item["section"] as! String) )

            print("TOTAL OF CLASSES: \(arrayLength)");

            for item in dataArray
            {

                classes.append(ClassSchedule(course: "TEMP", code: "TEMP", section: "TEMP", days:"TEMP", start:"TEMP", end:"TEMP", building:"TEMP", room:"TEMP", total: arrayLength) )

                classes[count].course = (item["subject"] as! String)
                classes[count].code = (item["catalog_number"] as! String)
                classes[count].section = (item["section"] as! String)
                print("Subject: \(classes[count].course!) \(classes[count].code!)");
                print("Section: \(classes[count].section!)");
              // print("Section: \(section_numb)");

                let subjectArray = item["classes"] as! NSArray

                for item2 in subjectArray{

                    let dateDictionary = item2["date"] as! NSDictionary
                    //let startTime = dateDictionary["start_time"]!
                    //self.performSelectorOnMainThread("updateIPLabel:", withObject: startTime, waitUntilDone: false)
                    //let endTime = dateDictionary["end_time"]!
                    classes[count].days = (dateDictionary["weekdays"] as! String)
                    classes[count].start = (dateDictionary["start_time"] as! String)
                    classes[count].end = (dateDictionary["end_time"] as! String)
                    string1 = classes[count].start!
                    print("Weekdays: \(classes[count].days!)");
                    print("START TIME: \(classes[count].start!)");
                    print("End Time: \( classes[count].end!)");


                    let locationDictionary = item2["location"] as! NSDictionary
                    classes[count].building = (locationDictionary["building"] as? String)
                    classes[count].room = (locationDictionary["room"] as? String)
                    print("Building: \(classes[count].building) \(classes[count].room)");


                    count += 1
                    print(count)
                    print("")

                }

            }
            //let subject = dataArray["subject"]
        }

    } catch {
        print("bad things happened")
    }

    //print("ASDIAWDWD: \(classes[0].section)")
   // print("ASDIAWDWASDASDD: \(classes[1].section)")
    completion(classes: classes)
    }).resume()

解决方案

It's hard to tell what you're trying to do exactly, but you shouldn't be using any asynchronous methods within your table view datasource methods. These methods should only represent data you already have. Seems like you could just replace that entire getTime block with just cell.textLabel?.text = "\(classes[indexPath.row].section!)".

这篇关于斯威夫特:tableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆