code-8
code-8

Reputation: 58652

Hide a certain Table Row - Swift 4

I have

a Table that contains 5 places.

var places =  ["lat": "42.61964890000001", "country": "United States", "name": "39 Cosgrove St", "lon": "-71.3031683"]
[["lat": "42.6285025", "country": "United States", "name": "138 B St", "lon": "-71.3288776"], ["lat": "42.6334255", "country": "United States", "name": "137 Pine St", "lon": "-71.3321021"], ["lat": "42.6225646", "country": "United States", "name": "98 Marriner St", "lon": "-71.31320219999999"], ["lat": "42.6252232", "country": "United States", "name": "48 Houghton St", "lon": "-71.3243308"], ["lat": "42.61964890000001", "country": "United States", "name": "39 Cosgrove St", "lon": "-71.3031683"]]

enter image description here

I want

to prevent showing the active place from the list in my table.

I try

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    
    if places[indexPath.row]["name"] != nil && places[indexPath.row]["name"] != places[activePlace]["name"] {
        cell.textLabel?.text = places[indexPath.row]["name"]
    }
    
    return cell
}

I got

the text to hide, but it seems to leave an empty space which look really bad.

enter image description here

I hope

someone can shed some lights on this.

Upvotes: 0

Views: 557

Answers (2)

Shehata Gamal
Shehata Gamal

Reputation: 100503

This

if places[indexPath.row]["name"] != nil && places[indexPath.row]["name"] != places[activePlace]["name"] {
    cell.textLabel?.text = places[indexPath.row]["name"]
}

causes the return of an empty cell ( if not a dequeued one ) , you need ro remove that info from the data source array ( may to retain it in another var or array if there are many of active ones ) and reload the table , you can use this

places = places.compactMap{ $0["name"] }

to remove the nils from the begining


Declare 2 temporary vars like

var tem:[String:String]? // pls create a model don't use array of dics
var activeIndex:Int? 

suppose you change active in a method which gives the new active index say it's neActiveIndex then you would do

if let oldValue = tem && oldActi = activeIndex { // is it the first process 
  places.insert(oldValue,at:oldActi)  // return it to it's place
}

// remove the new index 

tem = places.remove(at: neActiveIndex) // remove also returns the deleted object 
activeIndex = neActiveIndex

then reload the table

If the active index doesn't change and it's only a 1 time show then don't retain anything just remove the active index inside viewDidLoad and the table itself will be reloaded upon vc initiation

Upvotes: 2

Duncan C
Duncan C

Reputation: 131418

Your thinking is wrong. You need to remove the entry from your data model.

Say you have an array places that contains all your places.

You could change your table view to use a variable tablePlaces as it's data model

var tablePlaces: [[String, String]]

Then, to populate tablePlaces with your places, but remove the active place:

tablePlaces = places
tablePlaces.remove(at: activeIndex)
tableView.reloadData()

Upvotes: 1

Related Questions