Bar*_*lle 5 uitableview ios swift
我正在尝试构建一个面向对象的iOS应用程序,我在从一个swift类文件调用表重新加载(ui交互)时遇到问题.
如果我在没有对象的情况下工作并在InterfaceController的viewDidLoad方法中写下所有内容,那么一切正常......但如果我把它放到类中则不行.
我正在使用异步请求来从Web服务加载json数据.收到数据后,我将此数据用作表格视图的数据源.似乎tableview在启动后没有数据初始化,因此在完成异步请求后在tableview上调用reload()是必要的.
所以这里有详细信息:
主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
}
Run Code Online (Sandbox Code Playgroud)
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()
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试将self变量放入构造函数的构造函数中,它会抛出编译错误
var fhem :FHEM = FHEM(tableview : self)
Run Code Online (Sandbox Code Playgroud)
如果我尝试将其UITableView直接放入构造函数中,它也不起作用
var fhem :FHEM = FHEM(tableview : myTableView)
Run Code Online (Sandbox Code Playgroud)
我是否沿着完全错误的路径使用物体并与ui进行交互?
您可以在异步任务完成时发布通知:
NSNotificationCenter.defaultCenter().postNotificationName("refreshMyTableView", object: nil)
Run Code Online (Sandbox Code Playgroud)
将该通知的观察者添加到您的DeviceOverview类方法viewDidLoad:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "refreshList:", name:"refreshMyTableView", object: nil)
Run Code Online (Sandbox Code Playgroud)
并添加将在您的DeviceOverview类中触发的方法
func refreshList(notification: NSNotification){
myTableView.reloadData()
}
Run Code Online (Sandbox Code Playgroud)