0

As shown in image, initially I need to hide last 2 rows (my profile and update profile) in my table view when the user fills the form accessed via the "Become a merchant" row. I need to hide the "Become a Merchant" row and show the last two rows. This is done by Bool function(true/false).

check image

How could I do this?

var arrdata = ["Home", "Orders", "Change Password", "Log Out", "Become A Merchant", "My Profile", "Update Profile"]

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var isMerchantProfile: Bool = UserDefaults.standard.bool(forKey: "isFillMerchant")
        if isMerchantProfile == true {
            return 4
        }

        return arrdata.count
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Aleesha
  • 124
  • 2
  • 21
  • Possible duplicate of [How to insert new cell into UITableView in Swift](https://stackoverflow.com/questions/31870206/how-to-insert-new-cell-into-uitableview-in-swift) – shim Nov 21 '18 at 15:30

3 Answers3

1

Remove / add the rows you want to show from your data array and then use the table view functions insertRowsAt / deleteRowsAt. Do bulk updates using the performBatchUpdates function.

Refer to the UITableView documentation.

I would also recommend in this case using an enum to define your rows and rather than raw strings representing the titles of the rows.

e.g. (inside your view controller class is fine)

enum Row {
    case home, orders, changePassword, logout, becomeMerchant, myProfile, updateProfile

    var title: String {
        switch self {
        case .home:
            return NSLocalizedString("Home", comment: "Row title")
        // … etc
        }
    }
}

Then it's a bit easier to find the rows you want to remove / where to insert your rows and keep your code clean.

shim
  • 9,289
  • 12
  • 69
  • 108
0

I would adjust the data/array when reloadTable is called to match the expected behavior.

var arrdata = ["Home", "Orders", "Change Password", "Log Out", "My Profile", "Update Profile"] and then later:

var arrdata = ["Home", "Orders", "Change Password", "Log Out", "Become A Merchant", "My Profile", "Update Profile"] when you want this to show.

Alex Bailey
  • 793
  • 9
  • 20
0

I think first you need to be able to establish whether or not the user has filled the form out. Maybe using an enum type with different user levels to display different content. So maybe something like this.

enum UserType {
   case basic
   case merchant
}

Then when you load the data into the table view just switch on UserType and load the proper data set. That would allow you to use this implementation elsewhere throughout your app as well if you needed to add/remove data/views based on user type. So your menus would look something like this:

let basicUserMenu = ["Home", "Orders", "Change Password", "Log Out", "Become Merchant"]
let merchantUserMenu = basicUserMenu + ["My Profile", "Update Profile"]

And then your table view implementation would look something like this where userType is your stored UserType enum value. By default it would be .basic but after the user fills out the form successfully changes to .merchant

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch userType {
        case .basic:
            return basicUserMenu.count
        case .merchant:
            return merchantUserMenu.count
    }
 }

Then do the same thing for cellForTowAtIndexPath

Tyler Rolfe
  • 194
  • 1
  • 9