【发布时间】:2015-04-22 06:17:21
【问题描述】:
我正在尝试构建一个面向对象的 iOS 应用程序,但在从我的 swift 类文件之一调用表重新加载(UI 交互)时遇到问题。
如果我在没有对象的情况下工作并在我的 InterfaceController 的 viewDidLoad 方法中编写所有这些东西,那么一切正常......但如果我将它放入类中则不会。
我正在使用异步请求来从 web 服务加载 json 数据。收到数据后,我将此数据用作表格视图的数据源。 好像tableview是在启动后初始化的,没有数据,所以在完成异步请求后,需要在tableview上调用reload()。
下面是详细信息:
主表控制器
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 交互时沿着完全错误的路径行走?
【问题讨论】:
-
您可以为您的表格视图重新加载发布通知,并向您的表格视图控制器添加一个观察者以触发那里的 reloadData
-
有意思,有例子吗?
标签: ios uitableview swift