How Swift Passes Closure to Arguments

Asked 2 years ago, Updated 2 years ago, 104 views

I would like to detect on Swift that the UITableView has been loaded.
http://qiita.com/corocorococoro/items/1c80681cd7e1fddc3ba3
See the for information on

extension UITableView {
 ・・・
}

I tried to write it in , but the above reference URL is

-(void) reloadDataAndWait:(void(^)(void))waitBlock{
    self reloadData;
    if(waitBlock){
        waitBlock();
    }
}

I don't know how to write in Swift.

swift objective-c

2022-09-30 19:53

1 Answers

If you transplant it directly to Swift, it will be as follows.

extension UITableView {
    func reloadDataAndWait(waitBlock:(()->())?){
        reloadData()
        if let waitBlock = waitBlock {
            waitBlock()
        }
    }
}

The usage is as follows:

tableView.reloadDataAndWait{ 
    NSLog("numberofsection%d", self.tableView.numberOfSections)
}

As you can see in the comments in StackOverflow's answer further down the link, this code is now available in

tableView.reloadData()
NSLog("numberofsection%d", self.tableView.numberOfSections)

and reloadData() followed by processing.
So there's no point in creating such a method.


2022-09-30 19:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.