我正在尝试构建一个面向对象的 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/