How to add select from map option in tableViewCell, Google map, Swift - swift

I am using google map on my current project. To search places when I click on a textField a tableView appear below and I am populating the cell with GMSAutocompletePrediction object. So far its working fine and no problem so far. Now I want to add a cell like "Select From Map", when someone tap one the cell a marker will appear in the map. I can able to put the marker and additional map feature but how can I add that last cell "Select From Map" ? My code below:
var myFilterPredictions = [GMSAutocompletePrediction]()
Then:
func didAutocomplete(with predictions: [GMSAutocompletePrediction]) {
self.myFilterPredictions.removeAll()
print("GMSAutocompletePrediction *********")
for i in predictions {
self.myFilterPredictions.append(i)
}
tableView.reloadData()
}
The populating the tableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myFilterPredictions.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style:.subtitle, reuseIdentifier:"searchCell")
let predictions = myFilterPredictions[indexPath.row]
cell.textLabel?.attributedText = predictions.attributedPrimaryText
cell.detailTextLabel?.attributedText = predictions.attributedFullText
return cell
}
My search result looks similar to the image below:
I want to add "Select from map" in the last cell.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0{
return myFilterPredictions.count
}
else{
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = UITableViewCell()
if indexPath.section == 1{
cell.textLabel = ""Select From Map""
return cell
}else{
cell = UITableViewCell(style:.subtitle, reuseIdentifier:"searchCell")
let predictions = myFilterPredictions[indexPath.row]
cell.textLabel?.attributedText = predictions.attributedPrimaryText
cell.detailTextLabel?.attributedText = predictions.attributedFullText
return cell
}
}
You also use Group table for that
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
// Removes extra padding in Grouped style
return CGFloat.leastNormalMagnitude
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
// Removes extra padding in Grouped style
return CGFloat.leastNormalMagnitude
}

Related

Swift tableview 'method heightForHeaderInSection' makes section text raise

When heightForHeaderInSection and/or heightForFooterInSection is called, the section text raises above it's first row. I attached a screenshot to show the issue. How can I increase the section spacing without the section header being affected?
#IBOutlet weak var tableView: UITableView!
let fruits =
["apple","pear","orange","caramel","peach","fruit","fruits"]
let numbers = ["1", "2"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
if section == 0 {
return fruits.count
} else {
return numbers.count
}
}
func tableView(_ tableView: UITableView, titleForHeaderInSection
section: Int) -> String? {
if section == 0 {
return "Animals"
} else {
return "Numbers"
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:
"cell1", for: indexPath)
if indexPath.section == 0 {
cell.textLabel?.text = fruits[indexPath.row]
} else {
cell.textLabel?.text = numbers[indexPath.row]
}
return cell
}
These are the 2 methods in question:
func tableView(_ tableView: UITableView, heightForHeaderInSection
section: Int) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, heightForFooterInSection
section: Int) -> CGFloat {
return 100
}
You are setting the height of the header to 100. See here:
func tableView(_ tableView: UITableView, heightForHeaderInSection
section: Int) -> CGFloat {
return 100 // Instead try returning 30
}
func tableView(_ tableView: UITableView, heightForFooterInSection
section: Int) -> CGFloat {
return 100 // If you are not using a footer - return .zero
}
I got the answer from another post.
Just add:
tableView.sectionHeaderTopPadding = 100 // or whatever number you want.
There's also another way.
Click on of these ->
You can choose Grouped or the rounded design 'Inset Grouped'
After you can use the method 'heightForHeaderInSection' to select the specific section/s.

How to insert one Cell into the first section of UITableView Swift

I have created a UITableView with two sections. Now I am trying to populate one section. When I do action, my program populates both sections at the same time with the same text. What I am trying to do is to populate the first section, and then eventually move one of the cell from Section 1 to Section 2. So far I got here:
extension ListVC: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
randomCount = notes.count
return notes.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CustomNoteCell.reuseId) as! CustomNoteCell
cell.set(with: notes[indexPath.row])
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
sections.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section < sections.count {
return sections[section]
}
return nil
}
}
I believe it needs to happen in cellForRowAt but I have no clue how to specify section. Any help and clues appreciated. Many thanks ;)
Change the code of numberOfRowInSection to this:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 { //first section
randomCount = notes.count
return notes.count
}else{
return 0
}
}

Scroll to bottom TableView with custom section and cell Swift

My tableview has custom sections and cells. Every time the user scroll to the bottom it will call the server and load additional datas. My problem is how do I detect the last section so it could load more data? Each section contain 1 to 3 rows. My willDisplay wasn't working properly and the refreshControl.beginRefreshing() indicator didn't show too. Thanks!
let refreshControl = UIRefreshControl()
//MARK - Header Sections
func numberOfSections(in tableView: UITableView) -> Int {
return tableData[segmentedControlIndex].count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as! HomeSectionTableViewCell
let model = events[segmentedControlIndex][section]
cell.setup(model: model)
.
.
.
return cell.contentView
}
//MARK - Cells
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData[segmentedControlIndex][section].rows.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "detailCell") as! HomeTableViewDetailCell
.
.
.
cell.detailTitleLabel?.text = tableData[segmentedControlIndex][indexPath.section].rows[indexPath.row].detailTitleLabelString
cell.detailSubtitleLabel?.text = tableData[segmentedControlIndex][indexPath.section].rows[indexPath.row].detailSubtitleLabelString
return cell
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.000001
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell: HomeTableViewDetailCell = tableView.cellForRow(at: indexPath) as? HomeTableViewDetailCell {
cell.detailTitleLabel?.numberOfLines = cell.detailTitleLabel?.numberOfLines == 0 ? 1 : 0
tableView.reloadData()
}
}
//MARK - TableView add more data
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.section == tableView.numberOfSections - 1 {
self.refreshControl.beginRefreshing()
self.page = self.page + 1
self.getMoreData() { result in
self.refreshControl.endRefreshing()
print("Load More Bets Data reload: \(result == true ? "Success":"Failure")")
}
}
}

How to populate UITableView with 2 sections

My issue is with the NumberOfRowsInSection that should read two values.
let sections = ["Apples", "Oranges"]
override func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//Option A)
if apples.count & oranges.count != 0 {
return apples.count & oranges.count
} else {
return 0
}
//Option B)
if sections[0] == "Apples" {
return apples.count
}
if sections[1] == "Oranges"{
return oranges.count
}
return 0
}
None of the options work, crashing because it doesn't get the amount of things of one of each other... on the CellForRowAt.
Plus somebody knows how to get the title of those sections as well?
numberOfRows will be called for each section so you need to return the correct value based on the current section being queried:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return section == 0 ? apples.count : oranges.count
}
You need a similar pattern in cellForRowAt and all of the other data source/delegate methods:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", indexPath: indexPath
if indexPath.section == 0 {
cell.titleLabel.text = apples[indexPath.row]
} else {
cell.titleLabel.text = oranges[indexPath.row]
}
return cell
}
This is simplified and makes lots of assumptions but it gives you the right idea.
For the headers, you need:
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
This answer also assumes you just have the two sections based on two array variables. This is far from ideal. You really should have a single data structure with all of the proper structure. Then no assumptions need to be made. You can add more sections and rows and not have to change any table view code.

How to display data correctly in spacing table view cells

Only the first element from the array is displayed in my spacing cells.
I have no problems displaying data in simple cells without spacing, but when I need spacing cells and change my code, only the first item is displayed
let test = ["1","2","3"]
func numberOfSections(in tableView: UITableView) -> Int {
return test.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
let cellSpacingHeight: CGFloat = 10
return cellSpacingHeight
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.clear
return headerView
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SellerPlacesCell", for: indexPath) as! SellerPlacesCell
cell.backgroundColor = UIColor.white.withAlphaComponent(0.5)
cell.namePlace.text = test[indexPath.row]
return cell
}
How to fix it?
if in
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return 1
}
return 1
i replace return 1 to test.count have this:
Replace
cell.namePlace.text = test[indexPath.row]
with
cell.namePlace.text = test[indexPath.section]
the spacing is for the fact that you display sections , so you need to access the array with section not row