简体   繁体   English

Swift 3 /如何在全局函数中调用tableView.reloadData()?

[英]Swift 3 / How to call tableView.reloadData() in global function?

I'm trying to refactor a function to have it globally available. 我正在尝试重构一个函数以使其全局可用。 At the end of the function, a UITableView needs to be reloaded. 函数结束时,需要重新加载UITableView I was thinking about passing the VC, but I still get 我当时正在考虑通过VC,但是我仍然得到了

Value of type 'UIViewController' has no member 'tableView' 类型'UIViewController'的值没有成员'tableView'

I know that I get that error because tableView is not part of a UIViewController by default, but how do I bypass it? 我知道我收到该错误,因为默认情况下tableView不是UIViewController的一部分,但是如何绕过它呢?

func downloadAvatar(_ userForAvatar: String, vc: UIViewController) {

    let whereClause = "objectId = '\(userForAvatar)'"
    let dataQuery = BackendlessDataQuery()
    dataQuery.whereClause = whereClause

    let dataStore = backendless?.persistenceService.of(BackendlessUser.ofClass())
    dataStore?.find(dataQuery, response: { (users : BackendlessCollection?) in

        let withUser = users?.data.first as! BackendlessUser
        let imgURL = withUser.getProperty("Avatar") as? String
        avatare.setValue(imgURL, forKey: userForAvatar)
        vc.tableView.reloadData() // problem here

    }) { (fault : Fault?) in
        print("error: \(fault)")
    }
}

Help is very appreciated. 非常感谢您的帮助。

'UIViewController' has no member 'tableView' 'UIViewController'没有成员'tableView'

It tells everything ! 它说明了一切! Your vc is of type UIViewController , obviously it has no member named tableView . 您的vcUIViewController类型,显然没有名为tableView成员。

You have used the parameter vc only once and its for reloading table. 您仅使用参数vc及其重新加载表一次。 So instead of passing it, try passing the tableveiew reference itself. 因此,与其传递它,不如尝试传递tableveiew引用本身。

Because of some reasons, if you cant do this and you should use ViewController itself, try create a protocol, implement it in all of your VCs and use that as type. 由于某些原因,如果您不能执行此操作,则应使用ViewController本身,请尝试创建一个协议,并在所有VC中实现该协议并将其用作类型。 tableview should be a member of that protocol tableview应该是该协议的成员

This is basic programming. 这是基本编程。 Hope this helps 希望这可以帮助

Instead of have having vc: UIViewController as an argument, you should just have tv: UITableView instead. 与其使用vc: UIViewController作为参数,不如使用tv: UITableView Then instead of passing the view controller, you pass it's tableView. 然后,不传递视图控制器,而是传递它的tableView。 Although if this isn't working, you could try a completion block instead. 尽管如果这样做不起作用,您可以尝试使用完成块。

func downloadAvatar(_ userForAvatar: String, completion: @escaping () -> ()) {

    let whereClause = "objectId = '\(userForAvatar)'"
    let dataQuery = BackendlessDataQuery()
    dataQuery.whereClause = whereClause

    let dataStore = backendless?.persistenceService.of(BackendlessUser.ofClass())
    dataStore?.find(dataQuery, response: { (users : BackendlessCollection?) in

        let withUser = users?.data.first as! BackendlessUser
        let imgURL = withUser.getProperty("Avatar") as? String
        avatare.setValue(imgURL, forKey: userForAvatar)
        completion()

    }) { (fault : Fault?) in
        print("error: \(fault)")
    }
}

Then assuming you're calling this method from the respective view controller, you can reload the tableView data within the completion block. 然后,假设您要从各自的视图控制器调用此方法,则可以在完成块内重新加载tableView数据。

downloadAvatar("$uid") { [weak self] in
     self?.tableView.reloadData()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM