I have a TableView that receives an array of string identifiers, what i need to do is to set an identifier for every cell that i have and associate it to a variable called "cellPressedId", but when i do that, i just obtain the last value of the array
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let selectedCell = tableView.cellForRow(at: indexPath) {
if let selectedCell = tableView.cellForRow(at: indexPath) {
var cellPressed = self.cellPressedId
}
}
What I need to do is to set this identifier to every cell and when i pressed this cell obtain the identifier
when a cell is selected you have access to the indexPath of the selected cell via this function:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
and I'm sure you have access to your array of data (array of strings you mentioned that) you can access selected elements like this :
let selectedItem = myArray[indexPath.row]
Related
I'm not sure how to configure my custom cells to use a set in a tableView. I want to be able to display my cells but I get the syntax error Cannot call value of non-function type 'Set' in my cellForRowAt with my itemFav, how would I configure my current index to use a set?
var favSet = Set<CurrentPlayers>()
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "favcell", for: indexPath) as! FavCell
let itemFav = favSet(indexPath.row) //How do I configure this to a set
cell.update(with: itemFav)
return cell
}
As I understand, you need access by index. Try this:
favSet[favSet.index(favSet.startIndex, offsetBy: indexPath.row)]
I have an enum that will indicate the identifier and class to use for a tableViewCell.
enum Settings: String {
case Delete = "deleteCell"
var cellType: UITableViewCell.Type {
switch self {
case .Delete: return DeleteCell.self
}
}
}
In my tableView rowForIndexPath I am able to get the cell with the reuseIdendifyer, but it won't recognize the setting object to get the class. The error here is Use of undeclared type 'setting', specifically on the end of the line.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let setting = Settings.enabledSettings[indexPath.row]
return tableView.dequeueReusableCell(withIdentifier: setting.rawValue, for: indexPath) as! setting.cellType
}
It's not bad now with just the one object, but this will be growing soon and it's mostly the same thing, just changing the class name. Is it possible to pull the class type from another source like this so I can keep the rowForIndexPath function short and sweet, or do I need to do something like this?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let setting = Settings.enabledSettings[indexPath.row]
var cell = tableView.dequeueReusableCell(withIdentifier: setting.rawValue, for: indexPath)
if setting == .Delete { cell = cell as! DeleteCell }
return cell
}
When I call the tableView function I get the error in the title. My question is why won't the function take the two arguments even though they are of the requested type? Again I'm a newbie so please forgive me if the answer is obvious.
class TableViewController: UITableViewController {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell") as! UITableViewCell
let name = Array(shopItems.keys) [indexPath.row]
let cost = Array(shopItems.values) [indexPath.row]
cell.textLabel?.text = name + String(cost)
return cell
}
}
When I call the function like this:
"TableViewController.tableView(shopTableView, IndexPath: NSIndexPath)" I get the error: "Argument labels '(_:, IndexPath:)' do not match any available overloads"
Try use
let name = shopItems.keys[indexPath.row]
Instead of
let name = Array(shopItems.keys) [indexPath.row]
Better don't use force wrap, when it's not nesesery.
Try change
let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell") as! UITableViewCell
to
guard let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell") as? UITableViewCell else {
return UITableViewCell()
}
EDIT: As #Sh_Khan say replace
func tableView(_ tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell {
to
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
There is an easy and quick way to figure out yourself the proper overload
Select the entire method and press ⌘/ to comment out the code.
On the top level of the class type cellForRow. The first item in the code completion list is the proper method.
Press Return to insert the correct method.
Comment in the wrong method by selecting it again and pressing ⌘/.
Copy and paste the body of the wrong method into the correct one.
Delete the rest of the wrong method.
I am coding my first application in Swift. Right off the bat I am sorry if this is a duplicate as I have looked at others errors and wasn't really sure how to insert the help they got into my code.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return textView.text.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let myText = Int(entertextTextView.text!)
cell.textLabel?.text = "\(myText?[myText.index(myText.startIndex, offsetBy: indexPath.row)])"
return cell
}
I get a "Type 'Int' has no subscript members" error. I am trying to add a new cell into a textview for each character in a textview.
If you are adding a character to every cell, you shouldn't need to convert it to a Int. Only when it is a string will you be able to access a character using indices.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let myText = entertextTextView.text!
cell.textLabel?.text = "\(myText[myText.index(myText.startIndex, offsetBy: indexPath.row)])"
return cell
}
Also, it doesn't matter if your content is a number or not, which is why i'm assuming you changed it to an Int. Unless you are performing arithmetic operations or similar operations on the number, you can just let the content be a String.
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)
}