How to find particular position object from array in swift 3 - swift

I inserted some objects in my below finalArray array. Now I want to display them in tableList so that each position object I need to find. How can I write that statement can some one help me please?
My code:-
var forcast:Forecast?
var finalArray = [Forecast]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
forcast = finalArray.index(indexpath);
}

NSIndexPath has section and row
You need to access based on the indexPath.section or indexPath.row based on how you are categorising your data on tableView with the use tableView datasource
Ex:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
forcast = finalArray.index(indexpath.row);//
}

Try this :
var finalArray = [Forecast]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let forcast = finalArray[indexPath.row]
}
CollectionTypes documentation
var row: Int
The value of the row element of the index path.
var section: Int
The value of the section element of the index path.
IndexPath documentation

Related

Duplicates in tableview section Swift

I amm trying to put some datas into categories into a tableView. Pizzas into pizza category, burgers into burger etc, but i have every item duplicate ( Look here Picture . What can be the problem ?
Struct :
struct Food {
var photoKeyRestaurant: String
var foodName: String
var foodDescription: String
var restaurantName: String
var priceFood: Int
var typeFood: String
}
Table View
func numberOfSections(in tableView: UITableView) -> Int {
return food.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return food[section].foodName.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FoodTableViewCell", for: indexPath) as! FoodTableViewCell
// cell.delegate = self
let mancare = food[indexPath.section]
let storageRef = Storage.storage().reference()
let photoRef = storageRef.child(mancare.photoKeyRestaurant)
cell.foodImage.sd_setImage(with: photoRef)
cell.descriptionLabel.text = mancare.foodDescription
cell.foodNameLabel.text = mancare.foodName
cell.priceLabel.text = "\(mancare.priceFood) lei"
cell.foodImage.layer.borderWidth = 1
cell.foodImage.layer.masksToBounds = false
cell.foodImage.layer.borderColor = UIColor.black.cgColor
cell.foodImage.layer.cornerRadius = cell.foodImage.frame.height/2
cell.foodImage.clipsToBounds = true
//Fac ca imaginea sa fie cerc - finish
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 120
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return food[section].typeFood
}
If somone will ask, im retrieving the data from the firestore, but there s no problem there, because the data is retrieved succesfuly.
Anyone know what can be the problem ? Thanks
It is interesting that your code for cellForRowAt never actually mentions the indexPath.row. It refers only to the indexPath.section. How can you get and display data corresponding to this row, which you are supposed to configure in cellForRowAt if you do not in some way refer to the question of what row this is?
Perhaps there is some other mechanism going on that I don't see, but it seems to me that this one fact alone explains why each section just shows the same data over and over rather than different data for each row.
You have 2 big problems with this code.
You have the wrong array of data fo sections.
in the func cellForRowAt you cant return indexPath.section.
With first. You need to use 2-dimensional arrays like:
var foodCategorys: [FoodCategory] = [[categoryName, Food],[categoryName, Food]]
So you need another struct to contain this 2-dimensional array:
struct FoodCategory {
var categoryName: String
var listFood: [Food] = [] // Food is the struct that you currently have
}
So the data will display in tableView is an array FoodCategory. With numberOfSections will return a count of an array FoodCategory. And numberOfRowsInSection will return a count of a listFood into FoodCatogory.
func numberOfSections(in tableView: UITableView) -> Int {
return foodCategorys.count //array of listFood
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return foodCategorys[section].listFood.count
}
And second, in cellForRowAt, when you want to get the index of list data. you need to call foods[indexPath.section].listFood[indexPath.row]. The func need row not section
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FoodTableViewCell", for: indexPath) as! FoodTableViewCell
//cell.delegate = self
let mancare = foods[indexPath.section].listFood[indexPath.row] //Remeber that, the array food we are using in this line is array of FoodCategory
I think you need to research with key word "section in uitableview swift", or watch this video https://www.youtube.com/watch?v=AHY09z-XS9s
So, that all. I hope you will understand how tableview and section work

Nil is returned everytime I try to access textLabel of UITableViewCell

I am trying to get the text of the UITableViewCell in my
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedCell = tableView.cellForRow(at: indexPath)
let cellLabel = selectedCell!.textLabel!.text
print(cellLabel)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let musicCell = music[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "MusicCell") as! MusicCell
cell.setMusic(music: musicCell)
return cell
}
but its returning nill.
MusicListView is my UIViewController and I have extended it
extension MusicListView: UITableViewDataSource, UITableViewDelegate {
}
Any help will be greatly appreciated :)
So as suggested by others I accessed the required data from the data model.
I wrote a little function to access the data model
func findMusic(id : Int) -> String {
currentTitle = songList[id].title!
return currentTitle
}
and called it everytime a user tapped on a cell
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
currentTitle = findMusic(id: indexPath.row)
playSong(title: currentTitle)
}

Tableview creates five rows on one row?

I have a tableView with a custom cell that I have told should give me 5 cells in return of this custom cell. The question now is that I am running the app and getting five rows on one row. I have changed from default size of cell to custom but that is nearly the only thing I have done. So now I wonder what can create this kind of problem?
The code of my tableView looks like this.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "theoryCell") as! theoryTVC
return cell
}
So the problem is you are using custom cell with custom height but you are not setting height for row in table view method.
Try this:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 100.0 //Choose your custom row height
}

tableview loading invisible rows as well , cellforrowat index path getting celled for hidden rows

Tableview loading invisible rows as well , cellForRowAtIndexPath getting celled for hidden rows
func numberOfSections(in tableView: UITableView) -> Int {
return ordersArray.count;
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let selectedOrder = ordersArray[indexPath.section]
return CGFloat(180 + (30 * selectedOrder.lineItems().count ));
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(indexPath.row == 1)
{
let deliveryShipChargesCustomCell =
tableView.dequeueReusableCell(withIdentifier: "DeliveryShipChargesCustomCell", for: indexPath) as! DeliveryShipChargesCustomCell
return deliveryShipChargesCustomCell;
}
else
{
let orderCustomCell = tableView.dequeueReusableCell(withIdentifier: "OrdersCustomCell" , for: indexPath) as! OrdersCustomCell
return orderCustomCell;
}
}
problem with this code snippet is that it gets load row that are not even visible for example if order array count is 10. and there are two rows in each section , initially tableview (_ tableView: UITableView, cellForRowAtIndexPath: IndexPath) gets called for 20 times.
Any thoughts?

can't use my function for viewdidload Use of unresolved identifier 'getSegue' swift

cant call func for viewdidload
CODE
Use of unresolved identifier 'getSegue'
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! konuCell
func getSegue() {
cell.dersAdiLabel.text = gelenDersAdi
}
return cell
}
Basically, You written your function in the cellForRowAtIndexPath this will call nested Function but you have to write this at class scope.
In short writegetSegue() function at class level scope just like that:
func getSegue() {
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! konuCell
cell.dersAdiLabel.text = gelenDersAdi
return cell
}
To access getSegue, move it outside the scope of tableView(_:cellForRowAt:), e.g.:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
[...]
}
func getSegue() {
[...]
}
...but since getSegue would require a cell that it can modify, this is not going to work the way you want. I'd suggest keeping the code in cellForRowAt and reload the table view in viewDidLoad.