Custom TableViewCell with SwipeCellKit - swift

I have a problem with the understanding of the pod SwipeCellKit.
I have this simple setup (Firebase for retrieving the data)
On my :
class MyFriendsViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var MyFriendsTableview: UITableView!
var ref: DatabaseReference!
var MyFriendslist = [MyFriendsModel]()
And I have my cellForRowAt like this :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomFriends", for: indexPath) as! MyFriendsTableViewCell
// let cell = tableView.dequeueReusableCell(withIdentifier: "CustomFriends") as! SwipeTableViewCell
// cell.delegate = self
When I try to implement the swipeCellKit, I loose my references from my MyFriendTableViewCell (UIimage,label references) if I put :
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomFriends") as! SwipeTableViewCell
cell.delegate = self
Anyone knows how to unblock this ?
Thanks!

Just change this:
class MyFriendsTableViewCell: UITableViewCell { ...
To this:
class MyFriendsTableViewCell: SwipeTableViewCell { ...
We've just inherited from SwipeTableViewCell class. After this change, you will be able to use your MyFriendsTableViewCell properties and SwipeTableViewCell properties.
Updated code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomFriends", for: indexPath) as! MyFriendsTableViewCell
cell.delegate = self
...
cell.yourMyFriendsTableCellProperty = ...
return cell
}

Related

How to return array of tuples in a tableview?

I want to create and return an array of tuples in a tableView function but I am not sure how. I believe one method is to do so by decomposing the tuple but I'm not sure how to execute in this case. I know the last tableView is incorrect as it doesn't return (String, String) that is just my attempt.
class RideHistoryViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
let rideHistory: [(String,String)] = [("Driver: Joe, 12/29/2021", "$26.50"),
("Driver: Sandra, 01/03/2022", "$13.10"),
("Driver: Hank, 01/11/2022", "$16.20"),
("Driver: Michelle, 01/19/2022", "$8.50")]
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self,forCellReuseIdentifier:"TableViewCell")
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Code Here
return self.rideHistory.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Code Here
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath)
cell.textLabel?.text = self.rideHistory[indexPath.row]
return cell
}
You can access that tuple value by there index(0,1,2,3).
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Code Here
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath)
cell.textLabel?.text = self.rideHistory[indexPath.row].0
print(self.rideHistory[indexPath.row].0) // Driver: Joe, 12/29/2021
print(self.rideHistory[indexPath.row].1) // $26.50
return cell
}

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

Setting imageView in cell

i am new to Swift and I have seen tons of guides on the topic. However in my case, it's working. I have a custom cell file:
class FileCell: UITableViewCell {
#IBOutlet weak var firstName: UILabel!
#IBOutlet weak var cellImage: UIImageView!
func updateImage(name: String) {
cellImage.image = UIImage(named: name)
}}
In the view controller I am using "willDisplay" function as follows:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "FileCell") as! FileCell
let user = users[indexPath.row]
if user.email.isEmpty {
//cell.cellImage.image = UIImage(named: "folder.png")
cell.updateImage(name: "folder.png")
} else {
//cell.cellImage.image = UIImage(named: "file.png")
cell.updateImage(name: "file.png")
}
}
where i try to change imageView in cell depending on the data incoming to cell. However, the images either don't display at all or cells are not showing up.
Thanks in advance for tips on what i am doing wrong.
You should not dequeue a new cell, since you already have one.
Just skip the first statement:
// let cell = tableView.dequeueReusableCell(withIdentifier: "FileCell") as! FileCell
But one question: Why are you using the willDisplay delegate method? I would suggest to set up the cell in tableView(_:cellForRowAt:):
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "FileCell") as! FileCell
let user = users[indexPath.row]
cell.firstName.text = user.name // or something
if user.email.isEmpty {
cell.updateImage(name: "folder.png")
} else {
cell.updateImage(name: "file.png")
}
return cell
}

Thread 1 EXC error

I've been creating a firebase database but I have one error that I just can't get past. It says:
thread 1 exc_bad_instruction (code=exc_i386_invop subcode=0x0)'
It's probably something simple that I'm just looking past. Any Ideas?
Here's the code & the error is on the line of code:
//
// Database.swift
// intern
//
// Created by Lani Daniels on 8/20/17.
// Copyright © 2017 Lani Daniels. All rights reserved.
//
import UIKit
import Firebase
import FirebaseDatabase
struct PostStruct {
let title: String
let message: String
}
class DatabaseViewController: UITableViewController {
var posts: [PostStruct] = []
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let label1 = cell.viewWithTag(1)as! UILabel
label1.text=posts[indexPath.row].title
// ERROR BELOW:thread 1 exc_bad_instruction (code=exc_i386_invop subcode=0x0)
let label2 = cell.viewWithTag(2)as! UILabel
label2.text=posts[indexPath.row].message
return cell
}
}
For the first error:
let cell = tableview.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
Change tableview to tableView
For the second one:
It seems like you're trying to access to the outlet that might be not a UILabel. check this by making optional biding like:
let label2 = cell.viewWithTag(2) as? UILabel
and check that label using breakpoint. It might be nil.
Also it is better to use a custom cell class with two labels as outlets and then dequeue that custom cell in the cellForRowAt method:
class CustomCell: UITableViewCell {
#IBOutlet weak var firstLabel: UILabel!
#IBOutlet weak var secondLabel: UILabel!
}
And then
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? CustomCell
cell?.firstLabel.text=posts[indexPath.row].title
cell?.secondLabel.text=posts[indexPath.row].message
return cell!
}
And don't forget to set that custom class for the cell in the interface builder

Use of undeclared type 'ContactsTableViewCell'

I am working on to fetch phone contacts. But I got an error like use of undeclared type ContactsTableViewCell. I tried UITableViewCell but it didn't work. What should I do ?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as! ContactsTableViewCell
let data = dataArray![indexPath.row] as! Data;
cell.lblName.text = data.name
cell.imgContact.image = data.image
return cell
}
First you should make a class named ContactsTableViewCell which inherit UITableViewCell or if you already created it be sure you added this class to your targets.