edit certain section in viewForFooterInSection

Hey guys I'm having some struggles when trying to understand how to edit certain section for a viewForFooterInSection

note the edit occurs when the user types something and this function gets call, that is already managed

override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        return prepareFooterView(tableView: tableView, section: section)
    }

the question is how can I access certain section for me to edit the title on it?

the function for footherview is:

func prepareFooterView(tableView: UITableView, section: Int) -> UIView?{
        guard let tableView = tableView as? LightTableView else { return UIView() }
        guard section <= TableSection.all.count else { return UIView() }

///the section is obtained here
let section = TableSection.all[section]
    
    var title = ""
    switch section {
    case .detailsFooterView:
        title = "card_details_section_footer".localized
    case .nickname:
        if nickname?.isValidNickName == false {
            return prepareSpecificMessageInFooterView(with: "card_nickname_section_footer_error".localized, errorMessage: true)
        }
        title = "card_nickname_section_footer".localized
    case .fastlink:
        title = "earn_rewards_section_footer".localized
    default:
        title = ""
    }
    
    var footerView = tableView.standardFooterView(withTitle: title, textAlignment: .left, accessibilityIdentifier: section == .nickname || section == .fastlink || section == .detailsFooterView ? "NicknameFooterViewID" : nil, labelPadding: UIEdgeInsets(top: 12, left: 16, bottom: 0, right: -16))

    return section == .nickname || section == .fastlink || section == .detailsFooterView ? footerView : nil
}

I would like to edit the case .fastlink: when a flag is turned ON

the section here:

num TableSection: Int {
        
        case details
        case detailsFooterView
        case nickname
        case fastlink
        case scan

        static var all: [TableSection] = isUs ? [.details, .detailsFooterView, .nickname, .fastlink] : [.details, .detailsFooterView, .nickname, .fastlink, .scan]
    }

I guess this is not clear for me because this is running live instead of reloading the entire screen for example indexPath.row == 1

Replies

What about this:

create a global var

var newTitle = "earn_rewards_section_footer".localized

When you switch flag, ask user for the newTitle in a dedicated TextField.

Then use it in prepareFooterView

    case .fastlink:
        if flag.isOn {
          title = newTitle
        } else {
            title = "earn_rewards_section_footer".localized
        }

Note: you can overlay this new textField over the section title to mimic a direct change in section header. Or more simply, do it in an alert when you switch flag on.

  • thank you so much the problem was I wasn't reloading the correct section so the switch case the section 3 was never reached, but now is being reloaded properly

Add a Comment