blocking phone number in call kit - ios

I'm trying to using CallKit to add a feature to my app to add some phone numbers to blacklist!
the code below is my whole view!!
class BlacklistViewController: UIViewController ,UITableViewDataSource, UITableViewDelegate {
var phoneNumbersArrCoreData = [BlockedPhoneNumbers]()
var listPhoneNumbers:[CXCallDirectoryPhoneNumber] = []
#IBOutlet weak var TableView: UITableView!
#IBOutlet weak var BtnAddO: UIButton!
#IBOutlet weak var EntPhonenumber: UITextField!
#IBAction func BtnAddA(_ sender: Any) {
if !(EntPhonenumber.text?.isEmpty)!
{
let blackedPhonenumbers_CoreData = BlockedPhoneNumbers(context: PersistanceService.context)
blackedPhonenumbers_CoreData.phoneNumber = Int64.init(EntPhonenumber.text!)!
PersistanceService.saveContext()
getCoreData()
TableView.reloadData()
}
}
var coreData = [BlockedPhoneNumbers]()
func getCoreData()
{
listPhoneNumbers.removeAll()
let fetchRequest : NSFetchRequest<BlockedPhoneNumbers> = BlockedPhoneNumbers.fetchRequest()
do
{
let FetchedResultFromDB = try PersistanceService.context.fetch(fetchRequest)
coreData = FetchedResultFromDB
print("============\n===========\n")
if coreData.count > 0
{
for i in 0..<coreData.count
{
listPhoneNumbers.append(coreData[i].phoneNumber)
}
}
print("============\n===========\n")
}
catch{
print("gettin blocked number from db got error")
}
}
override func viewDidLoad() {
BtnAddO.layer.cornerRadius = 5
BtnAddO.layer.borderColor = UIColor.white.cgColor
BtnAddO.layer.borderWidth = 0.8
EntPhonenumber.attributedPlaceholder = NSAttributedString(string: "Enter a phone number to block",attributes: [NSAttributedString.Key.foregroundColor: UIColor.lightText])
getCoreData()
super.viewDidLoad()
view.backgroundColor = UIColor.init(red: 25/255, green: 28/255, blue: 46/255, alpha: 1)
TableView.delegate = self
TableView.dataSource = self
}
func beginRequest(with context: CXCallDirectoryExtensionContext) {
getCoreData()
let blockedPhoneNumbers: [CXCallDirectoryPhoneNumber] = listPhoneNumbers
for phoneNumber in blockedPhoneNumbers.sorted(by: <) {
context.addBlockingEntry(withNextSequentialPhoneNumber: phoneNumber)
}
context.completeRequest()
}
//MARK: - TableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listPhoneNumbers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BlackListCell") as? BlackListTableViewCell
cell?.ContactImg.layer.masksToBounds = true
cell?.mainView.layer.cornerRadius = 10
cell?.mainView.backgroundColor = UIColor(red: 42/255, green: 48/255, blue: 66/255, alpha: 1)
cell?.ContactImg.layer.cornerRadius = 5
cell?.ContactImg.image = UIImage(named: "Blocked")
cell?.unBlock.imageView?.image = nil
cell?.unBlock.setTitle("UNBLOCK", for: UIControl.State.normal)
cell?.unBlock.layer.cornerRadius = (cell?.unBlock.frame.size.height)!/2
cell?.SetUnblockBtn {
I get the error here,below
let context:NSManagedObjectContext = PersistanceService.context
context.delete(self.phoneNumbersArrCoreData[indexPath.row] as NSManagedObject)
self.phoneNumbersArrCoreData.remove(at: indexPath.row)
print("data deleted!!!")
}
cell?.phoneNumber.text = String(listPhoneNumbers[indexPath.row])
return cell!
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 85
}
}
to explain the code, I save each number that user will enter in a core data(entityName: BlockedPhoneNumbers). I'm not sure even if this is the right way to save numbers that they need to be blocked or not!!
when the user presses the button I save the number and it works fine( but I'm not sure if this is the right way or not!!).
and in getCoreData I get the core data and show them in a table view. which shows that core data works fine! but when user wanna unblock the contact and presses the button in CELL of the table view, I get an error and app crash and it says:
Thread 1: Fatal error: Index out of range
my problems are:
why do I get this error?
2.as I can not find any tutorial for callKit I believe that I'm doing this job wrong.
could anyone help me with this?

You have too many arrays:
listPhoneNumbers which contains your integer numbers
coreData which contains your Core Data items
phoneNumbersArrCoreData which could contain your Core Data items, but you don't add anything to it.
As a result, phoneNumbersArrCoreData is empty. When you try and remove an object from the empty array you get an array bounds exception.
You should eliminate two of the three arrays.
class BlacklistViewController: UIViewController ,UITableViewDataSource, UITableViewDelegate {
var blockedNumbers = [BlockedPhoneNumbers]()
#IBOutlet weak var TableView: UITableView!
#IBOutlet weak var BtnAddO: UIButton!
#IBOutlet weak var EntPhonenumber: UITextField!
#IBAction func BtnAddA(_ sender: Any) {
if !(EntPhonenumber.text?.isEmpty)!
{
let blackedPhonenumbers_CoreData = BlockedPhoneNumbers(context: PersistanceService.context)
blackedPhonenumbers_CoreData.phoneNumber = Int64.init(EntPhonenumber.text!)!
PersistanceService.saveContext()
getCoreData()
TableView.reloadData()
}
}
func getCoreData()
{
let fetchRequest : NSFetchRequest<BlockedPhoneNumbers> = BlockedPhoneNumbers.fetchRequest()
do
{
let FetchedResultFromDB = try PersistanceService.context.fetch(fetchRequest)
blockedNumbers = FetchedResultFromDB
print("============\n===========\n")
}
catch{
print("gettin blocked number from db got error")
}
}
override func viewDidLoad() {
BtnAddO.layer.cornerRadius = 5
BtnAddO.layer.borderColor = UIColor.white.cgColor
BtnAddO.layer.borderWidth = 0.8
EntPhonenumber.attributedPlaceholder = NSAttributedString(string: "Enter a phone number to block",attributes: [NSAttributedString.Key.foregroundColor: UIColor.lightText])
getCoreData()
super.viewDidLoad()
view.backgroundColor = UIColor.init(red: 25/255, green: 28/255, blue: 46/255, alpha: 1)
TableView.delegate = self
TableView.dataSource = self
}
//MARK: - TableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return blockedNumbers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BlackListCell") as? BlackListTableViewCell
cell?.ContactImg.layer.masksToBounds = true
cell?.mainView.layer.cornerRadius = 10
cell?.mainView.backgroundColor = UIColor(red: 42/255, green: 48/255, blue: 66/255, alpha: 1)
cell?.ContactImg.layer.cornerRadius = 5
cell?.ContactImg.image = UIImage(named: "Blocked")
cell?.unBlock.imageView?.image = nil
cell?.unBlock.setTitle("UNBLOCK", for: UIControl.State.normal)
cell?.unBlock.layer.cornerRadius = (cell?.unBlock.frame.size.height)!/2
cell?.SetUnblockBtn {
let context:NSManagedObjectContext = PersistanceService.context
context.delete(self.blockedNumbers[indexPath.row] as NSManagedObject)
self.phoneNumbersArrCoreData.remove(at: indexPath.row)
print("data deleted!!!")
}
cell?.phoneNumber.text = blockedNumbers[indexPath.row].phoneNumber
return cell!
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 85
}
}
The code to actually load data into the Call Kit block list needs to go into a CallKit extension in your app. You will need to use an application group to share the Core Data store with the extension.

Related

List view populated with firebase query results problem SWIFT IOS

Hello I'm trying to do a List that is populated with query results from Firebase Database
I've got constantly some problems and i have no clue what can i try to do next ive looked through internet to help me do this but found nothing, can you help me?
Thanks in Advance
Here is the code with errors
class BoatListViewContoller: UIViewController, UITableViewDelegate, UITableViewDataSource {
var title = [""] // Here error is saying "Property 'title' with type '[String]' cannot override a property with type 'String?'"
var lenght:Int?
func readProducts(){
let db = Firestore.firestore()
db.collection("products").getDocuments(){
querySnapshot, err in
if let err = err {
print("Error getting documents: \(err)")
} else {
self.lenght = querySnapshot!.count
for document in querySnapshot!.documents{
self.title.append(document.data()["title"] as! String) // Here i got a error saying "No exact matches in call to subscript"
}
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return lenght!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath)
cell.textLabel!.text = title[indexPath]
cell.backgroundColor = UIColor.init(red: 212/255, green: 255/255, blue: 241/255, alpha: 1)
return cell
}
private var myTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad();
let displayWidth: CGFloat = self.view.frame.width
let displayHeight: CGFloat = self.view.frame.height
myTableView = UITableView(frame: CGRect(x: 0, y: 0, width: displayWidth, height: displayHeight))
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
myTableView.dataSource = self
myTableView.delegate = self
myTableView.backgroundColor = UIColor.init(red: 212/255, green: 255/255, blue: 241/255, alpha: 1)
view.addSubview(myTableView)
}
}
First error:
UIViewController already has a property named title:
open var title: String?
You just need to rename var title = [""] to var titles = ["], or something different than title.
Second error:
You might try document.get("title") as! String (borrowed the idea from https://stackoverflow.com/a/54601354/3227743), or you might try
let data = document.data()
let title = data["title"]
or
self.title.append((document.data())["title"] as! String)
Miscellaneous:
You would also need to call myTableView.reloadData() after parsing every document.
Nitpick: length instead of lenght. Also, you actually don't need that since you could (should) just use titles.count instead.

Swift Xcode TableViewCell's Not Loading First Time

I have a Button which Segues to my TableViewController and when I click the button for the first time, the TableView Doesn't Load but when I go back and click again, my Data Loads.
I tried to set the breakpoints but the tableview functions don't load? Any reason for this?
EDIT:
This is my Code For The TableView, if any other code is required tell me.
class ServicesDisplay: UITableViewController {
#IBOutlet var MainTitle: UILabel!
#IBOutlet var image: UIImageView!
let db = Firestore.firestore()
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
performSegue(withIdentifier: "seque", sender: self)
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 260
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let jsonFbPic = (jsonFile["SecondScreen"])
let test = ((jsonFbPic["Services Image"]))
let count = ((test["Image\(myIndex)"]))
if count.count == 0 {
return serviceTextArray.count
} else {
return count.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomServicesCell
cell.serviceText?.text = serviceTextArray[indexPath.row]
//Variables
let Json = jsonFile["SecondScreen"]
let MainTitleJson = Json["Text"]
let MainTitleSize = CGFloat(Int("\(MainTitleJson["Size"])")!)
//Main Title:
cell.serviceText.font = UIFont(name: "\(MainTitleJson["Font"])", size: MainTitleSize)
cell.serviceText.textColor = hexStringToUIColor(hex: "\(MainTitleJson["Color"])")
cell.serviceImage?.loadImagesFromCacheWithUrlString(myIndex: indexPath.row, labelName: serviceTextArray[indexPath.row], CellImage: cell.serviceImage)
cell.selectionStyle = .none
return cell
}
public func hexStringToUIColor(hex: String) -> UIColor {
var cString: String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
var rgbValue: UInt64 = 0
if cString.hasPrefix("#") {
cString.remove(at: cString.startIndex)
} else if cString.count != 6 {
return UIColor.black
}
Scanner(string: cString).scanHexInt64(&rgbValue)
return UIColor(red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0, green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0, blue: CGFloat((rgbValue & 0x0000FF)) / 255.0, alpha: CGFloat(1.0))
}
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async(execute: { () -> Void in
self.tableView.reloadData()
})
}
override func viewDidAppear(_ animated: Bool) {
}
// MARK: - Table view data source
}
Sorry for posting it as an answer but I am also new to stackOverFlow. It Is happening because you are performing segue before the data is fetched from the server. Perform segue as a completion after data fetch is completed. Please let me know if that was a case!

SwipCellKit swift 5 - unable to swipe to launch the dele menu

I'm using Xcode 11.2 and Swift 5 with the latest version of SwipeCellKit.
In my code, I have a UITableView with cells in it. I wish to add swipe action from the right, which will let me use two actions: delete and edit.
For now, I'm trying only the delete action. However, the swipe doesn't seem to be working, I get no actions at all.
What am I missing here?
Here's my code:
CartTableViewCell
import UIKit
import SwipeCellKit
class CartTableViewCell: SwipeTableViewCell
{
#IBOutlet weak var mainView: UIView!
#IBOutlet weak var productName: UILabel!
#IBOutlet weak var productImage: UIImageView!
#IBOutlet weak var priceForKg: UILabel!
#IBOutlet weak var quantity: UITextField!
#IBOutlet weak var subTotal: UITextField!
override func awakeFromNib()
{
super.awakeFromNib()
self.layer.borderWidth = 1
self.layer.cornerRadius = 25
self.layer.borderColor = self.layer.backgroundColor
self.mainView.layer.borderWidth = 1
self.mainView.layer.cornerRadius = 25
self.mainView.layer.borderColor = self.mainView.layer.backgroundColor
self.quantity.layer.borderWidth = 1
self.quantity.layer.borderColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
self.quantity.layer.cornerRadius = 5
self.subTotal.layer.borderWidth = 1
self.subTotal.layer.borderColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
self.subTotal.layer.cornerRadius = 5
}
override var frame: CGRect {
get {
return super.frame
}
set (newFrame) {
var frame = newFrame
frame.origin.y += 4
frame.size.height -= 2 * 5
super.frame = frame
}
}
override func setSelected(_ selected: Bool, animated: Bool)
{
super.setSelected(selected, animated: animated)
}
}
CartViewController
import UIKit
import SwipeCellKit
class CartViewController: UIViewController
{
#IBOutlet weak var cartTableView: UITableView!
var listOfProducts : [Product] = []
override func viewDidLoad()
{
super.viewDidLoad()
self.cartTableView.delegate = self
self.cartTableView.dataSource = self
self.cartTableView.separatorStyle = .none
self.cartTableView.allowsSelection = false
self.cartTableView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.subTotalView.layer.borderWidth = 1
self.subTotalView.layer.borderColor = self.subTotalView.layer.backgroundColor
self.subTotalView.layer.cornerRadius = 10
let cellNib = UINib(nibName: "CartTableViewCell", bundle: nil)
self.cartTableView.register(cellNib, forCellReuseIdentifier: "cartProductCell")
}
}
extension CartViewController : UITableViewDelegate, UITableViewDataSource, SwipeTableViewCellDelegate
{
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]?
{
guard orientation == .right else { return nil }
let deleteAction = SwipeAction(style: .destructive, title: "Delete")
{
action, indexPath in
// handle action by updating model with deletion
}
// customize the action appearance
deleteAction.image = UIImage(named: "Delete")
return [deleteAction]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return listOfProducts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cartProductCell", for: indexPath) as! CartTableViewCell
return cell
}
}
I found the answer, if any of you guys are looking for it, the missing line is to set the delegate of the cell before returning it:
cell.delegate = self
As mentioned in this post:
SwipeCellKit Swipe to delete not being called

Swift - Segue to wrong index when UISearchBar is active

I am new to code, and I just can't find the solution to this problem. Most of the answers on the internet are deprecated or unanswered.
I want to search something into my searchbar, after that I want to press on one of the results. When I click on the result I want it to give me the corresponding information in another viewcontroller.
Here's my problem: When I click on a result, the information in the other viewcontroller doesn't correspond with the information that the result gave me. What he does gives me is the information that corresponds to the tableview. So, it segues with a wrong index. Does anyone knows what the solution is?
Here's my code (tableviewcontroller with searchbar):
var myIndex = 0
extension UIColor { //themakleur
static let candyGreen = UIColor(red: 71.0/255.0, green: 81.0/255.0, blue:
89.0/255.0, alpha: 1.0)}
var watjeberekend = ["Omtrek Cirkel","Oppervlakte Cirkel","Lengte
Boog","Oppervlakte Sector","Oppervlakte Bol"]
class scheikunde: UITableViewController, UISearchResultsUpdating {
var scheikundeformules = ["Omtrek Cirkel","Oppervlakte Cirkel","Lengte Boog","Oppervlakte Sector","Oppervlakte Bol"]
var searchcontroller : UISearchController!
var filterednamen = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.searchcontroller = UISearchController(searchResultsController: nil)
self.tableView.tableHeaderView = self.searchcontroller.searchBar
self.searchcontroller.searchResultsUpdater = self
self.searchcontroller.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
self.navigationItem.hidesBackButton = false;
self.navigationController?.isNavigationBarHidden = false
//navigationcontroller layout
navigationController?.navigationBar.barTintColor = UIColor(red: 71.0/255.0, green: 81.0/255.0, blue: 89.0/255.0, alpha: 1.0)
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
navigationController?.navigationBar.layer.borderColor = UIColor.candyGreen.cgColor
navigationController?.navigationBar.layer.borderWidth = 0
//searchcontroller layout
searchcontroller.searchBar.layer.borderWidth = 0
searchcontroller.searchBar.layer.borderColor = UIColor.candyGreen.cgColor
UISearchBar.appearance().barTintColor = .candyGreen
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .candyGreen
searchcontroller.searchBar.backgroundImage = UIImage(named: "themakleur.jpg")
let cancelButtonAttributes: [String: AnyObject] = [NSForegroundColorAttributeName: UIColor.white]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal)
}
func updateSearchResults(for searchController: UISearchController) {
self.filterednamen = self.scheikundeformules.filter { (naam : String) -> Bool in
if naam.lowercased().contains(self.searchcontroller.searchBar.text!.lowercased()) {
return true
}else{
return false}}
self.tableView.reloadData()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if !searchcontroller.isActive || searchcontroller.searchBar.text == "" {
return self.scheikundeformules.count
}else {
return self.filterednamen.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Custommath
cell.label.text = scheikundeformules[indexPath.row]
if !searchcontroller.isActive || searchcontroller.searchBar.text == "" {
//cell.textLabel?.text = self.namen[indexPath.row]
cell.label.text = self.scheikundeformules[indexPath.row]
}else{
//cell.textLabel?.text = self.filterednamen[indexPath.row]
cell.label.text = self.filterednamen[indexPath.row]
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
performSegue(withIdentifier: "segue", sender: self)
}
}
Here is my code from the viewcontroller:
import UIKit
class scheikundeformule: UIViewController{
#IBOutlet var uitleg6: UILabel!
#IBOutlet var formuleomschrijving: UILabel!
#IBOutlet var uitleg5: UILabel!
#IBOutlet var uitleg4: UILabel!
#IBOutlet var uitleg3: UILabel!
#IBOutlet var uitleg2: UILabel!
#IBOutlet var uitleg1: UILabel!
#IBOutlet var titlelabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
titlelabel.text = scheikundeformules[myIndex]
uitleg1.text = uitlegformules2[myIndex]
uitleg2.text = uitlegformules3[myIndex]
uitleg3.text = uitlegformules4[myIndex]
uitleg4.text = uitlegformules5[myIndex]
uitleg5.text = uitlegformules6[myIndex]
uitleg6.text = uitlegformules7[myIndex]
self.navigationItem.hidesBackButton = false;
self.navigationController?.isNavigationBarHidden = false
formuleomschrijving.text = watjeberekend[myIndex]
//keyboard weg deel 1
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(startscreen.dismissKeyboard))
//Uncomment the line below if you want the tap not not interfere and cancel other interactions.
//tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
Where is your function prepare for segue ? if you want to pass the data to the next view controller you need to send it in the fun prepare for segue, and remember that the indexpath.row needs to be used with the array that is the current datasource of the tableview, the solution will be something like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVC = segue.destination as? YourViewController {
if !searchcontroller.isActive {
destinationVC.name = filterednamen[myIndex]
} else {
destinationVC.name = scheikundeformules[myIndex]
} }
}
Add identifier for scheikundeformule view controller in storyboard like below :
Step 1:
Step 2:
change your tableview didSelectRow like below :
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
let scheikundeformuleController = self.storyboard?.instantiateViewController(withIdentifier: "scheikundeformule") as! scheikundeformule
scheikundeformuleController.myIndex = indexPath.row
self.present(scheikundeformuleController, animated: true, completion: nil)
}
Step 3:
You have to add var myIndex = 0 to your scheikundeformule controller. Like below :
class scheikundeformule: UIViewController{
var myIndex = 0
........Your code
}

UITableView only updating on scroll up, not down

I have a UITableView that updates when I scroll up, but it does not update when I scroll down. Furthermore, when it does update it occasionally seems to "skip" a cell and update the next one.
There are 6 total cells that should populate
I've created the UITableView in the storyboard, set my constraints for both the hashLabel and the creditLabel in storyboard
Here is the image of the initial TableView:
And upon scrolling up, when updated properly:
...and when scrolling up "misses" a cell:
and of course, the class:
class HashtagController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var model:ModelData!
var currentCell: UITableViewCell!
#IBOutlet var hashtagTableView: UITableView!
let basicCellIdentifier = "CustomCells"
override func viewDidLoad() {
super.viewDidLoad()
model = (self.tabBarController as CaptionTabBarController).model
hashtagTableView.delegate = self
hashtagTableView.dataSource = self
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "CherrySwash-Regular", size: 25)!, NSForegroundColorAttributeName: UIColor(red:27.0/255, green: 145.0/255, blue: 114.0/255, alpha: 1.0)]
configureTableView()
hashtagTableView.reloadData()
}
func configureTableView() {
hashtagTableView.rowHeight = UITableViewAutomaticDimension
hashtagTableView.estimatedRowHeight = 160.0
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//deselectAllRows()
hashtagTableView.reloadData()
}
override func viewDidAppear(animated: Bool) {
hashtagTableView.reloadData()
}
func deselectAllRows() {
if let selectedRows = hashtagTableView.indexPathsForSelectedRows() as? [NSIndexPath] {
for indexPath in selectedRows {
hashtagTableView.deselectRowAtIndexPath(indexPath, animated: false)
}
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return model.quoteItems.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return customCellAtIndexPath(indexPath)
}
func customCellAtIndexPath(indexPath:NSIndexPath) -> CustomCells {
var cell = hashtagTableView.dequeueReusableCellWithIdentifier(basicCellIdentifier) as CustomCells
setTitleForCell(cell, indexPath: indexPath)
setSubtitleForCell(cell, indexPath: indexPath)
return cell
}
func setTitleForCell(cell:CustomCells, indexPath:NSIndexPath) {
let item = Array(Array(model.quoteItems.values)[indexPath.row])[0] as? String
cell.hashLabel.text = item
}
func setSubtitleForCell(cell:CustomCells, indexPath:NSIndexPath) {
let item = Array(model.quoteItems.keys)[indexPath.row]
cell.creditLabel.text = item
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
/*currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!
var currentLabel = currentCell.textLabel?.text
var currentAuthor = currentCell.detailTextLabel?.text
model.quote = currentLabel!
model.author = currentAuthor!*/
}
}
class CustomCells: UITableViewCell {
#IBOutlet var hashLabel: UILabel!
#IBOutlet var creditLabel: UILabel!
}
As it turns out, the issue had to do with my estimatedRowHeight. In this case the row height was too large and it was effecting the way the table cells were being constructed.
So in the end I changed hashtagTableView.estimatedRowHeight = 160.0 to hashtagTableView.estimatedRowHeight = 80.0 and everything worked just fine.

Resources