iOS reloading a UITableView from a Swift class/object

前端 未结 3 1876
难免孤独
难免孤独 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

    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();
        var myDevicesSection = NSMutableDictionary()
        var deviceObjects = Array();
        var mappingSectionIndex = Array();
        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 = self.fhem!.sections[self.fhem!.mappingSectionIndex[section]] as Array
    
            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 =   self.fhem!.sections.objectForKey(sectionName) as? Array {
                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()
            self.sections = NSMutableDictionary()
            self.deviceNames = Array()
            self.mappingSectionIndex = Array()
            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.

提交回复
热议问题