iOS reloading a UITableView from a Swift class/object

前端 未结 3 1854
难免孤独
难免孤独 2020-12-17 02:53

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.

If I am working wit

相关标签:
3条回答
  • 2020-12-17 02:56

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

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

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

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

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

    func refreshList(notification: NSNotification){
        myTableView.reloadData()
    }
    
    0 讨论(0)
  • 2020-12-17 02:56

    Hi You can use Notification as suggested in Above answers otherwise you can use delegation to resolve this problem

    Delegate is doing something like that you done but in easy manner.

    Here you pass table view controller in you custom class.this thing you can do with delegate. create custom delegate method in your custom class. set delegate with table view controller object.

    here you don't required to take IBOutlet of your table view because your controller inherited from table view controller so it's view is table view

    import UIKit
    
    
    class DeviceOverview: UITableViewController,FHEMDelegate {
    
        @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")
        var fhem :FHEM?
        // THIS IS AN ATTEMPT TO give the class itself as PARAMETER
        // ALSO TRIED TO USE myTableView (IBOutlet)
    
    
        override func viewDidLoad() {
    
            super.viewDidLoad()
            fhem = FHEM ()
            fhem?.delegate = self
    
        }
    
    
        override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return 1;
        }
    
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //        var devicesInSection : Array<NSDictionary> = self.fhem!.sections[self.fhem!.mappingSectionIndex[section]] as Array<NSDictionary>
    
            return 5;
        }
    
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("device", forIndexPath: indexPath) as UITableViewCell
            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
        }
        func reloadDataOfTable()
        {
            self.tableView.reloadData()
        }
    }
    

    FHEM

    import UIKit
    protocol FHEMDelegate{
    
       func reloadDataOfTable()
    }
    class FHEM : NSObject {
        var devices : [NSDictionary]
        var sections : NSMutableDictionary
        var deviceNames : [String]
        var mappingSectionIndex : [String]
        //var myTableViewController : DeviceOverview
        var delegate :FHEMDelegate?
    
     override init(){
            self.devices = Array<NSDictionary>()
            self.sections = NSMutableDictionary()
            self.deviceNames = Array<String>()
            self.mappingSectionIndex = Array<String>()
            self.delegate = nil
            super.init()
    
        let url = NSURL(string: "http://google.com");
            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.delegate?.reloadDataOfTable()
                }
            }
        }
    }
    

    Here in FHEM class create one custom delegate method reloadDataOfTable that implement in your viewcontroller class so when got response of code it call that method and this method (reloadDataOfTable) content code for reload table data.

    0 讨论(0)
  • 2020-12-17 03:07

    I don't know much Swift, but I've been writing Objective-C for iOS for a while now. (In Objective-C at least) you must call init on super before accessing self, otherwise you're object isn't initialized correctly. You're also not assigning self to the result of super.init(), which is necessary in Objective-C, looks like it isn't in Swift though.

    tl;dr -- Move the call to super.init() to the very first line in your FHEM class' init method.

    0 讨论(0)
提交回复
热议问题