iOS 从 Swift 类/对象重新加载 UITableView

标签 ios uitableview swift

我正在尝试构建一个面向对象的 iOS 应用程序,但我在从我的一个 swift 类文件调用表重新加载(ui 交互)时遇到问题。

如果我在没有对象的情况下工作并在我的 InterfaceController 的 viewDidLoad 方法中编写所有这些东西一切正常......但如果我把它放入类中则不行。

我正在使用异步请求从网络服务加载 json 数据。收到数据后,我将此数据用作表格 View 的数据源。 看起来tableview在启动后就初始化了,没有数据,所以需要在完成异步请求后对tableview调用reload()。

下面是详细信息:

主表 Controller

import UIKit;


class DeviceOverview: UITableViewController {

@IBOutlet var myTableView: UITableView!

var myDevices = Array<String>();
var myDevicesSection = NSMutableDictionary()
var deviceObjects = Array<NSDictionary>();
var mappingSectionIndex = Array<String>();
var sharedUserDefaults = NSUserDefaults(suiteName: "group.barz.fhem.SharingDefaults")

// THIS IS AN ATTEMPT TO give the class itself as PARAMETER
// ALSO TRIED TO USE myTableView (IBOutlet)
var fhem :FHEM  = FHEM(tableview : self)

override func viewDidLoad() {

    super.viewDidLoad()

}


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return self.fhem.sections.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var devicesInSection : Array<NSDictionary> = self.fhem.sections[self.fhem.mappingSectionIndex[section]] as! Array<NSDictionary>
    return devicesInSection.count;
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("device", forIndexPath: indexPath) as! TableViewCell
    let sectionName : String = fhem.mappingSectionIndex[indexPath.section]
    if let devices : Array<NSDictionary> =   self.fhem.sections.objectForKey(sectionName) as? Array<NSDictionary> {
        let device = devices[indexPath.row]
        var alias : NSString?;
        if let attr : NSDictionary = device.objectForKey("ATTR") as? NSDictionary {
            alias = attr.objectForKey("alias") as? String
        }
        if (alias != nil) {
            cell.deviceLabel.text = alias as? String
        }else{
            if let deviceName : String = device.objectForKey("NAME") as? String {
                cell.deviceLabel.text = deviceName
            }
        }
    }
    return cell
}

FHEM 类

import UIKit


class FHEM: NSObject {

var devices : [NSDictionary]
var sections : NSMutableDictionary
var deviceNames : [String]
var mappingSectionIndex : [String]
var myTableViewController : DeviceOverview

init(tableview : DeviceOverview){
    self.devices = Array<NSDictionary>()
    self.sections = NSMutableDictionary()
    self.deviceNames = Array<String>()
    self.mappingSectionIndex = Array<String>()
    self.myTableViewController = tableview
    super.init()

    let url = NSURL(string: "xyz");
    var request = NSURLRequest(URL: url!);

    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {
        (response, data, error) in
        if let jsonData: NSData? = data {
            // do some great stuff
            //
            // this is an attempt to call the table view to reload
            self.myTableViewController.myTableView.reloadData()
        }
    }
}
}

如果我尝试将 self 变量放入我的构造函数的构造函数中,它会抛出编译错误

var fhem :FHEM  = FHEM(tableview : self)

如果我尝试将 UITableView 直接放入构造函数中,它也不起作用

var fhem :FHEM  = FHEM(tableview : myTableView)

我是否在使用对象并与 UI 交互时走完全错误的路径?

最佳答案

您可以在异步任务完成时发布通知:

NSNotificationCenter.defaultCenter().postNotificationName("refreshMyTableView", object: nil)

将观察者添加到您的 DeviceOverview 类方法 viewDidLoad 的通知中:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "refreshList:", name:"refreshMyTableView", object: nil) 

并添加将在您的 DeviceOverview 类中触发的方法

func refreshList(notification: NSNotification){
    myTableView.reloadData()
}

关于iOS 从 Swift 类/对象重新加载 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29789243/

相关文章:

ios - 将 Flutter Downloader 插件集成到 AppDelegate.swift 中

objective-c - 在没有for循环的情况下获取另一个数组中每个对象的属性值数组

ios - UITableViewController 和 SQLite 语句中的土耳其语字符

ios - 如何更新 Parse AppVersion

ios - 在 Swift 2.0 中对成员 'removeAll' 的引用不明确

ios - AFNetworking 2.0 向服务器发送参数和图像

html - iOS 中的两列选择微调器

ios - 如何快速制作多行表格 View 标题?

Swift 中的 UITableViewCell

swift - Firebase-observeSingleEventOfType(:withBlock:) being skipped