Search code examples
iosswiftuitableviewupdatescustom-cell

Update Uitableview cells button title


I have a UiTableview with custom cells. In each cell I have a button, the button's title is set to "Upload" when the cell is created. When click the button, it Uploads a project to the server and the button title becomes "Uploaded". And i want to have a button in the navigation bar, when pressed, it reset all the buttons' title (buttons in UITableview cells) to "upload", I've tried to use reloadData function, but it didn't work. I'm very new to iOS and swift, any help? Thanks in advance!


Solution

  • Table View Cells get reused, so we need some object, such as an array, to keep the state of each cell.

    As soon as the project item is loaded to the server - we need to update the item in the array at the corresponding index (based on cell's Index Path). Probably, each item in the array must be a Dictionary.

    So initially, the Dictionary's contents will be:

    {
        "project_name": "Project 1",
        "project_label": "Upload"
    }
    

    After upload is finished:

    {
        "project_name": "Project 1",
        "project_label": "Uploaded"
    }
    

    When you want to reset everything - you must iterate over each item in the array and change set the "project_label" key to "Upload".

    Finally, call reloadData().