1

I'm trying to add an icon (downward) when the user pulls the tableView to refresh. the icon shows(downward indicator) to the user. in this screenshot I want to dismiss this arrow when the loader is started and shows an arrow when the user starts to pull the tableView here my code is

    var refreshControl = UIRefreshControl()
  let arr = ["first row ","2nd row ","3rd row ","4th row ","5th row ","6th row ","7th row ","8th row ","9th row"]
    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        refreshControl.addTarget(self, action: #selector(self.refresh(_:)), for: .valueChanged)
        tableView.addSubview(refreshControl)
        tableView.rowHeight = 160
        
        
    }

    @objc func refresh(_ sender: AnyObject) {
        refreshControl.endRefreshing()
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arr.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = arr[indexPath.row]
       
        return cell
    }

but I want a downward arrow icon on collectionView before the refresh indicator

coceki
  • 173
  • 10
  • 3
    This may help you [customize refresh control](https://stackoverflow.com/questions/16588091/how-to-customize-uirefreshcontrol-with-different-image-and-position) – Ptit Xav Apr 12 '21 at 11:53
  • @PtitXav I want the loader as it is but add a down arrow on the tableView row which indicates the user that the user pulls the tableView to refresh – coceki Apr 13 '21 at 04:09
  • @coceki but that arrow should be always visible on 1st row in table view or it should be visible when user make pull refresh? – Łukasz Łabuński Apr 13 '21 at 08:53
  • @ŁukaszŁabuński yes, only visible on 1st row before refresh loader comes out – coceki Apr 13 '21 at 09:35
  • @coceki So making something like that is weird because it's normal for iOS users that they are using pull to refresh to refresh table content. But if it's necessary you could create if statement in `cellForRowAt` method for `indexPath.row == 0` and return the cell with an arrow. Also you need to remember to add 1 to the `numberOfRowsInSection` method. In case of showing/hiding that one you can try to do that in `refresh` method because this method is called when user start refreshing and in implementation of that method you are calling `endRefreshing` method to finish refreshing. – Łukasz Łabuński Apr 13 '21 at 10:38
  • To complement @Lukasz Labuński you can also have 2 sections : one with one row for the arrow and one for the data – Ptit Xav Apr 13 '21 at 12:16

1 Answers1

2

Here is a template for creating your custom class for UIRefreshControl:

class CustomRefreshControl: UIRefreshControl {
fileprivate var isAnimating = false
fileprivate let maxPullDistance: CGFloat = 150

override init() {
    super.init(frame: .zero)
    setupView()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func updateProgress(with offsetY: CGFloat) {
    guard !isAnimating else { return }
    // progress
    let _ = min(abs(offsetY / maxPullDistance), 1)
}

override func beginRefreshing() {
    super.beginRefreshing()
    isAnimating = true
}

override func endRefreshing() {
    super.endRefreshing()
    isAnimating = false
}

func setupView() {
     tintColor = .clear
          addTarget(self, action: #selector(beginRefreshing), for: .valueChanged)
     }
}
Alirza Eram
  • 134
  • 11