Swift - change height of mapview inside a tableView - swift

(Look at ##Edit##)
I'm a beginner. I used google but can't find a solution for my problem.
How can I change the height of my MapView inside the tableView?
My storyboard looks like:
click here I cant post pictures
I can change the size of the MKMapView however I want but nothing changed "in real/in the emulator".
I tried to change the size of the cells with:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if(indexPath.row == 0) {
return 500
}
return 75.0
}
But indexPath.row=0 ist the first entry "Name: Test"
Can somebody explain how and why I can't change the size? How can I change the size?
Thank u for help.
EDIT
I tried it with two different cells ("Map Cell" and "listAllBakeries") and now it works with the size.
When I click on an entry there isn't an update at the "Map Cell" but on the "listAllBakeries". More than one Map
The code:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//CODE TO BE RUN ON CELL TOUCH
//get the Location with latitude and longitude
var location = CLLocationCoordinate2D(latitude: latitude[indexPath.item], longitude: longitude[indexPath.item])
//resolution
var span = MKCoordinateSpanMake(0.003, 0.003)
var region = MKCoordinateRegion(center: location,span:span)
//here is the mistake
let cell2:ListPlacesTableViewCell = tableView.dequeueReusableCellWithIdentifier("MapCell", forIndexPath: indexPath) as! ListPlacesTableViewCell
//some Text about the Place
var annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = namesBakeries[indexPath.item]
annotation.subtitle = "some Text"
//Zooming inside the map
cell2.Map.setRegion(region, animated: true)
cell2.Map.addAnnotation(annotation)
}
I don't really understand this. I use the right identifier (MapCell) but the section/row is wrong. How can I use the right section?
Here is my code how I fill the rows/section
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if(indexPath.section == 0) {
let cell2:ListPlacesTableViewCell = tableView.dequeueReusableCellWithIdentifier("MapCell", forIndexPath: indexPath) as! ListPlacesTableViewCell
//insert MapStuff
//addAnnotations
//MapStuff
return cell2
} else {
//Standard-Labels with text
let cell:ListPlacesTableViewCell = tableView.dequeueReusableCellWithIdentifier("listAllBakeries", forIndexPath: indexPath) as! ListPlacesTableViewCell
cell.nameLabel?.text = namesBakeries[indexPath.row]
cell.adressLabel?.text = "Test 2"
return cell
}
}
Thank for help
(I'm sorry for my english but this isn't my mothertongue).

It looks like your map is above the uiTableView rather than a separate prototype cell in your table.
Set your uiTableView up with 2 prototype cells then put the map in the first.
Then, as you have done, you can test the value of indexpath.row.

Okay I found out.
The trick is to update always the same cell.
I defined an array. Then save my Cell2 in this array and just used this Object to update my map. (My english is awful I know, maybe my code will give more information.)
class ListPlacesTableViewController: UITableViewController {
var constantCell = [ListPlacesTableViewCell?](count: 1, repeatedValue: nil)
....
//other stuff here
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if(indexPath.section == 0) {
let cell2:ListPlacesTableViewCell = tableView.dequeueReusableCellWithIdentifier("MapCell", forIndexPath: indexPath) as! ListPlacesTableViewCell
constantCell[0] = cell2
//other stuff here
}
}
//Clickable Cell Function add:
constantCell[0]!.Map.setRegion(region, animated: true)
constantCell[0]!.Map.addAnnotation(annotation)
Thanks for help

Related

Trying to bind an output to my tableviewCell UI Element in RxSwift

I have an output that lives in my VM and based on some change I want my textfield that resides in my custom tableviewCell to change in some way. I am unsure about how to have my UItextfield that lives in the tableviewcell bind to my output.
let index = IndexPath(row: 0, section: 0)
if let cell = tbl.cellForRow(at: index) as? TableViewCell {
print(cell.textF.text)
// Do whatever you want
}
I am not sure when you want to get the data, but if you want to do this when the user touches the cell, just implement the following method of UITableViewDelegate:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? YourTableViewCellClass {
let cellLabelText = cell.yourTextField.text
// Do whatever you want with cellLabel
}
}

How to change the state of the image in cell made in xib?

There is a TableView and a "Locked" image, how do I get alpha = 0 when I click cell 1 in cell 2? There is the cellForRowAt function:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! TableViewCell
cell.commonInit("got_\(indexPath.item)", title: titleLessons[indexPath.item], sub: summary[indexPath.item], imageLock: imageZ[indexPath.item])
return cell
}
There is the didSelectRowAt function:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if unlockedRows.contains(indexPath.row) {
let nextIndex = indexPath.row + 1
if nextIndex < pdfArr.count {
let vc = DetailLessonsVC()
let ce = TableViewCell()
vc.pdfTitle = pdfArr[indexPath.row]
vc.numberList = pdfArr[indexPath.row]
vc.linkList = link[indexPath.row]
unlockedRows.append(indexPath.row + 1)
self.navigationController?.pushViewController(vc, animated: true)
self.tableView.deselectRow(at: indexPath, animated: true)
}
print("no")
}
}
TableViewCell is xib's custom class.
You need to access the cell, and change image property, you can access the cell in didselect method with this:
let cell = tableView.cellForRowAtIndexPath(indexPath)
In your cell's custom class (TableViewCell) create an outlet for your image. If I understand correctly you want to edit cell located in the row number N+1 when user taps on the cell located in the row above, right?
If that's the case then all you have to do is get a reference to the next cell in didSelectRowAt. You can do just that this way:
var nextCellsIndexPath = indexPath
nextCellsIndexPath.row += 1
// Use 'if let' in order to make sure the cell at that index path exists
if let nexCell = tableView.cellForRow(at: nextCellsIndexPath) {
nextCell.lockedImage.alpha = 0
}
Since you didn't specify the number of sections in your table view, I assumed it has just one.

Swift - UITableViewCell label position is shifted after scrolling

The Table View Cells are laid out correctly during the initial load, but when I scroll up or down, the labels are shifted to the left. I'm not sure what is going on here.
My code for the tableview cell is below.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let workout = self.workouts[indexPath.row] as? Workout
let cell = tableView.dequeueReusableCellWithIdentifier("WorkoutCell") as? WorkOutCell
cell!.textCell?.text = workout?.title
cell!.backgroundColor = workout?.color
cell!.countLabel.text = "\(indexPath.row+1)"
cell!.selectionStyle = UITableViewCellSelectionStyle.None
return cell!
}
Image:
Thanks much!
Is there any changes if use this instead?
let cell = tableView.dequeueReusableCellWithIdentifier("WorkoutCell", forIndexPath: indexPath) as? WorkOutCell

UIImage not appearing in prototype cells in UITableView (Swift)

I have created a UITableView Containing 10 cells, each of which have a UIImageView attached to them using auto-layout
The table is populated in my BackTableViewController by the code:
TableArray = ["Home Page", "Membership Card", "Recent News", "Fitness Schedule", "Training Log", "Pool Lap Calculator", "Martial Arts Schedule", "Pool Schedule", "Comments", "Acknowledgements"]
The issue is that none of these images appear in the table until the cell is selected while run. I am out of ideas as to why this is... anyone?
Thanks
Edit: BackTableViewController
import UIKit
class BackTableViewController: UITableViewController {
var TableArray = [String]()
var ImageArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
TableArray = ["Home Page", "Membership Card", "Recent News", "Fitness Schedule", "Training Log", "Pool Lap Calculator", "Martial Arts Schedule", "Pool Schedule", "Comments", "Acknowledgements"]
ImageArray = ["home-50.png","credit_card-50.png","info-50.png","math-50.png","news-50.png","report_card-50.png","weightlift-50.png"]
// Do any additional setup after loading the view.
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return TableArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(TableArray[indexPath.row], forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = TableArray[indexPath.row]
return cell
}
}
You only need one prototype cell with a reuse identifier. The clue is in the name "prototype". Think of them as blueprints all cells are based off.
In your view controller class (which should be a UITableViewDelegate if it is a UITableViewContoller) you specify the number of rows with this method:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return yourArray.count
}
This is the method that takes care of displaying the right info in the right row of the table.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//We create a cell from our prototype that we gave an identifier to in our storyborad.
let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell") as! UITableViewCell
//Create a single instance of an item from our items array. we use the indexpath.row that was passed into this method as the index for the array.
let item = yourArray[indexPath.row]
//You can go into your array and get the right info out regarding this row by using the indexPath.row
cell.textLabel?.text = item
cell.imageView?.image = UIImage(named: item)
//Finally this method returns the correctly configured cell fro the tableview to display.
return cell
}
Hope this helps.
Try giving "Center x" constraint instead of leading space for the image view, and you should give it width and height constraint also.
You need to set your cell identifier using Interface Builder in the Attributes Inspector:
Then in your cellForRowAtIndexPath you need to pass the cell identifier as you set before, something like this:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// set the identifier cell, not TableArray[indexPath.row]
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = TableArray[indexPath.row]
return cell
}
I hope this help you.

Trouble passing tableview cell value to delete cell

I am trying to pass a value to update a tableview cell. Problem is I am not able to get the value. The tableview cell code is:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell;
// Gets staff name and puts it in the cell title
var sectionTitle:String = aventurasUnicas.objectAtIndex(indexPath.section) as String
var sectionStaff:Array = porAventura[sectionTitle]!
let staffNic:String = sectionStaff[indexPath.row] as String
cell.textLabel?.text = staffNic
// Gets the subtitle
var subtituloAventura:String = "\(sectionTitle).\(staffNic)" as String
var sectionStaff2:Array = subtituloTabla[subtituloAventura]!
let staffNic2:String = sectionStaff2[0] as String
cell.detailTextLabel?.text = staffNic2
cell.accessoryType = UITableViewCellAccessoryType.DetailButton
return cell
}
And the code I am stuck with is:
override func tableView(tableView: UITableView?, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath?) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
// I AM STUCK HERE
}
}
I want to get the staffNic for the next part of the program. Thanks for your help
Why not go this way - the way you get it is the way you set it:
let sectionTitle:String = aventurasUnicas.objectAtIndex(indexPath.section) as String
let sectionStaff:Array = porAventura[sectionTitle]!
//the text of the deleted row based on your datasource
let staffNic = sectionStaff[indexPath.row] as String
Or get it as cell's text:
let cell = tableView.cellForRowAtIndexPath(indexPath)!
let staffNic = cell.textLabel!.text!