Search code examples
swiftuitableviewreloaddata

tableview.reloadData() does not repeat cellForRowAt function


I'm trying to create an app using Swift 4.

The purpose of the table is to use UITableViewCell.SelectionStyle.none if my a cell data has 0 count from the API.

And later, i used UIRefreshControl() to refresh the data by pulling down the table.

but whenever i pull down the table, the data refreshes but the style of the cell is not changing.

I already tried reloadData() but it does not work. The function does not reload the cellForRowAt.

Here is my cellForRowAt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "custom") as! AppsCustomCell
        cell.imglink = data[indexPath.row].imglink
        cell.title = data[indexPath.row].AppName
        cell.subtitle = data[indexPath.row].AppDesc
        cell.count = String(data[indexPath.row].count)
        cell.backgroundColor = UIColor.clear
        cell.layoutSubviews()

        if data[indexPath.row].boolPress == 0 || data[indexPath.row].count < 1 {
//        if false {

            cell.selectionStyle = UITableViewCell.SelectionStyle.none
            cell.contentView.isOpaque = false
            cell.mainImageView.alpha = 0.5
            cell.titleView.alpha = 0.5
            cell.subtitleView.alpha = 0.5
            cell.count = "0"

        } else {

            cell.count = String(data[indexPath.row].count)
            cell.countView.backgroundColor = UIColor.red
            cell.countView.textColor = UIColor.white

        }

        return cell

    }

I expect to reload the entire table including the styles of the cells whenever i use refresh to pull

EDIT : here is how i refresh my table

@IBOutlet weak var tableView: UITableView!
var refreshControl = UIRefreshControl()

override func viewDidLoad() {
        refreshControl.addTarget(self, action: #selector(refresh), for: UIControl.Event.valueChanged)
        tableView.addSubview(refreshControl) // not required when using UITableViewController
}

@objc func refresh() {
        // Insert code here to get data from the API.
        self.tableView.reloadData()
        self.refreshControl.endRefreshing()
}

EDIT: Here is the result in UI

This is the old data

AND HERE IS THE tableView if i pull down the table using UIRefreshControl()

Refreshed Table


Solution

  • You are adding the refresh control to the table view in the wrong way. Try below:

    override func viewDidLoad() {
        super.viewDidLoad()
        refreshControl.addTarget(self, action: #selector(refresh), for: UIControl.Event.valueChanged)
        tableView.refreshControl = refreshControl
    }
    

    Also, set the selection style when you have a count greater than 0.

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCell(withIdentifier: "custom") as! AppsCustomCell
        cell.imglink = data[indexPath.row].imglink
        cell.title = data[indexPath.row].AppName
        cell.subtitle = data[indexPath.row].AppDesc
        cell.count = String(data[indexPath.row].count)
        cell.backgroundColor = UIColor.clear
        cell.layoutSubviews()
    
        if data[indexPath.row].boolPress == 0 || data[indexPath.row].count < 1 {
    //        if false {
    
            cell.selectionStyle = UITableViewCell.SelectionStyle.none
            cell.contentView.isOpaque = false
            cell.mainImageView.alpha = 0.5
            cell.titleView.alpha = 0.5
            cell.subtitleView.alpha = 0.5
            cell.count = "0"
    
        } else {
            cell.selectionStyle = .default
            cell.count = String(data[indexPath.row].count)
            cell.countView.backgroundColor = UIColor.red
            cell.countView.textColor = UIColor.white
            cell.mainImageView.alpha = 1.0
            cell.titleView.alpha = 1.0
            cell.subtitleView.alpha = 1.0
        }
    
        return cell
    
    }