Populating table view with data - swift

so im trying to populate the rows in my table with the information stored in array of "events". where events is an object that has location and title data. for some reason im getting a bad instruction error thread 1 at the line thats commented. Any idea why?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {// this sets the title and subtitle to the Title and Location in the given EventPlan
let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.EventCellIdentifier, for: indexPath)
cell.textLabel?.text = eventsArray[indexPath.row].title // Error here
cell.detailTextLabel?.text = eventsArray[indexPath.row].location
return cell
}

in your number of rows for section data source call, make sure that you're returning eventsArray.count

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]

Configure TableView Cells With A Set

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

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.

Better performance events inside TableView cell

I am developing a Facebook like application, where if the post is too long, it gets cut down and at the end of it "See more" appears that has some click events. (I use FRHyperLabel for it)
Everything works fine, but I've got a question: If the formatting+event is decleared inside tableView: cellForRowAt indexPath: is it bad for the performance?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var message : message
var cell : MessageCell
let message = self.messages[indexPath.row]
// World Message
cell = tableView.dequeueReusableCell(withIdentifier: "essageCell", for: indexPath) as! MessageCell
cell.messageLabel.attributedText = message.limitMessageMax250char()
//Add link if needed
if message.count > appSettings.maxCharacters.message {
let handler = {
(hyperLabel: FRHyperLabel!, range: NSRange!) -> Void in
self.alert(title: "Test", message: "Alert: because clicked on link")
}
let text = cell.messageLabel.text!
let nsRange = text.calculateUnicodeScalar(start: text.unicodeScalars.count-8, len: 8) // See more is 8 characters
cell.messageLabel.setLinkFor(nsRange, withLinkHandler: handler)
}
return cell
}
Or should I do it in the Cell's file, when it's awakeFromNib + with delegate
IMO you don't want to do this in the for cellForRowAt call mostly because you want to make that call return quickly and also because you are only returning the formatted cell.
I would argue/recommend that you set your handler for the row when it is being initialized and that way the handler value is only being set once.
Remember that cellForRowAt is called multiple times as the user scrolls through and as cells come into view.
review the https://developer.apple.com/documentation/uikit/uitableviewdatasource/1614861-tableview for more information as to how cells are reused.

Xcode tables refresh on button click

I have a table which is populated with JSON. It works as a calendar. When the date is clicked, the table below should be populated with the events for that day.
These populate the tables after the data has been found but only does it once.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return(GlobalVar.TodaysEvents.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "eventCell", for: indexPath) as! WhitelistTableViewCell
cell.startTime.adjustsFontSizeToFitWidth = true
print(GlobalVar.TodaysEventsTimes[indexPath.row])
cell.startTime.text = (GlobalVar.TodaysEventsTimes[indexPath.row] as! String)
cell.Summary.text = GlobalVar.TodaysEvents[indexPath.row] as? String
return(cell)
}
I have seen other posts like this and they use:
self.tableView.reload()
However, when i try this, it doesn't recognise "tableView" and simply doesn't work.
Any advice would be appreciated, thanks in advance
create a reference to your tableView ( New Referencing Outlet ) and then do this :
yourTableView.reloadData()
I don't know where you are reload the tableview but i think you reload the table view at different thread. That's why please reload the table view after inserting the data in array.