Type 'Int' has no subscript members? - swift

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.

Related

Set String Identifier to Cell in Swift

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]

Swift Newbie Argument labels '(_:, IndexPath:)' do not match any available overloads

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.

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)
}

Error: "Unexpected nil index path in _canPerformAction:forCell:sender:, this should never happen."

I have a dynamic tableView with 2 prototype cells. I am using one of the cells for section header, the section header cell has it's own class. Data have been populated to these cells without problems.
I am getting this error message "Error: “Unexpected nil index path in _canPerformAction:forCell:sender:, this should never happen.” at runtime when I tap on the section header. Anyone knows how to get rid of this error? Thanks in advance!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MachineTableViewCell") as! MachineTableViewCell
if self.uptime.count == self.machines.count {
cell.GPUNumber.text = self.allGPUNumber[indexPath.section][indexPath.row]
}
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let header = tableView.dequeueReusableCell(withIdentifier: "header") as? HeaderTableViewCell
else {
return nil
}
let machine = machine[section]
header.name.text = machine.name + " - " + machine.ip + ":" + machine.port
return header
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
return 124
}
I initially missed this as well but if you take a step back and look at the return type you almost kick yourself.
For a regular cell the return type is UITableViewCell.
However for the header it's UIView.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")...
///
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableCell(withIdentifier: "header")...
///
return header
}
Therefore, what you need to do is return the contentView.
return header.contentView

Recording file name can not be displayed on the TableView cell

Audio Record
The recorded file does not show up on the TableView How can I show it?
I hope that the TableView Cell can show recording-yyyy-MM-dd-HH-mm-ss.m4a Recording function is normal, but the file name cannot be displayed in the TableView Cell
I hope that in the specified cell can also play I want to hear the recording
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return recordings.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = recordings[indexPath.row].lastPathComponent
return cell
}