iOS从Swift类/对象重新加载UITableView [英] iOS reloading a UITableView from a Swift class/object

查看:88
本文介绍了iOS从Swift类/对象重新加载UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to build a object oriented iOS app and I am having problems calling a table reload (ui interaction) from one of my swift class files.

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

If I am working without objects and write all that stuff in my InterfaceController's viewDidLoad method everything works... but not if I put that into classes.

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

I am using an asynchronous request in order to load json data from a webservice. After receiving the data I am using this data as data source for my table view. It seems the tableview is initialized after startup with no data, so it is neccessary to call reload() on the tableview after finishing the async request.

所以以下是详细信息:

Main TableController

Main TableController

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 Class

FHEM Class

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 变量放入构造函数的构造函数中,则编译错误

It throws compile errors if I try to put the self variable into the constructor of my constructor

var fhem :FHEM  = FHEM(tableview : self)

如果我也不行尝试将 UITableView 直接放入构造函数中

It also does not work if I try to put the UITableView directly into the constructor

var fhem :FHEM  = FHEM(tableview : myTableView)

我是否沿着完全错误的路径使用对象并与ui交互?

Am I walking along complete wrong path using objects and interacting with the ui?

推荐答案

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

You can just post a Notification when your async task finishes:

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

将一个观察者添加到您的DeviceOverview类方法viewDidLoad:

Add an observer to that notification to your DeviceOverview class method viewDidLoad:

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

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

and add the method that will be fired at your DeviceOverview class

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

这篇关于iOS从Swift类/对象重新加载UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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