Hide Show Section According to server response swift - arrays

I am developing an app which used table view with multiple section and multiple header view. Is it possible to hide randomly section according to server response, i.e.: I have 6 Multiple cells and header view. I got response from the server which show only 2 sections, 4 section and 6 section. I am trying to achieve this, but didn't get success. Here is my code below:
Code:
var allPermissionSection = ["A","B","C","D","E","F"] // default permission
override func numberOfSections(in tableView: UITableView) -> Int {
return totalSectionTable // total count of showing cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let modelPermission = mainArrayCheckValidations[0] as! MyAcountModel
if section == 0 {
return 1
}
// arraySectionName got from server check index if default section or server reponse index is equal to same then true this continue
if allPermissionSection[0] == arraySectionName[0] {
if arrayTblItems.count > 0 {
return arrayTblItems.count
}else if modelPermission.phoneNumbersEnabled == true {
return 1
}
}
if allPermissionSection[1] == arraySectionName[1] {
if arrayTblItemsForElectronic.count > 0 {
return arrayTblItemsForElectronic.count
}else if modelPermission.econsentEnabled == true {
return 1
}
}
if allPermissionSection[2] == arraySectionName[2] {
if arrayTblItemsForEmergnceContact.count > 0 {
return arrayTblItemsForEmergnceContact.count
}else if modelPermission.emergencyContactsEnabled == true {
return 1
}
}
return 0
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let modelPermission = mainArrayCheckValidations[0] as! MyAcountModel
if indexPath.section == 0 {
return UITableView.automaticDimension
}
if allPermissionSection[0] == arraySectionName[0] {
// if indexPath.section == 1 {
if arrayTblItems.count <= 0 {
if modelPermission.phoneNumbersEnabled == true{
return 40
}else{
return 0
}
}
// }
}
if allPermissionSection[1] == arraySectionName[1] {
// if indexPath.section == 2 {
if arrayTblItemsForElectronic.count <= 0 {
if modelPermission.econsentEnabled == true{
return 40
}else{
return 0
}
}
// }
}
if allPermissionSection[2] == arraySectionName[2] {
// if indexPath.section == 3 {
if arrayTblItemsForEmergnceContact.count <= 0 {
if modelPermission.emergencyContactsEnabled == true{
return 40
}else{
return 0
}
}
// }
}
return UITableView.automaticDimension
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: USER_DETAIL_CELL, for: indexPath) as! UserDetail_Cell
cell.updateWith(mainArrayCheckValidations[indexPath.row], indexPath.row, mainArrayCheckValidations.count, tblCellType)
return cell
}
if allPermissionSection[0] == arraySectionName[0] {
// if indexPath.section == 1 {
if arrayTblItems.count > 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: PHONE_CELL, for: indexPath) as! PhoneNumberDetailCell
cell.updateWith(arrayTblItems[indexPath.row], indexPath.row, arrayTblItems.count, tblCellType)
return cell
}else{
let cell = tableView.dequeueReusableCell(withIdentifier: DEFAULT_CELL, for: indexPath) as! Default_Custom_Cell
cell.lbl_Name.text = ""
return cell
}
//}
}
if allPermissionSection[1] == arraySectionName[1] {
// if indexPath.section == 2 {
if arrayTblItemsForElectronic.count > 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: CONSTENT_CELL, for: indexPath) as! Constent_Cell
cell.updateWith(arrayTblItemsForElectronic[indexPath.row], indexPath.row, arrayTblItemsForElectronic.count, tblCellType)
return cell
}else{
let cell = tableView.dequeueReusableCell(withIdentifier: DEFAULT_CELL, for: indexPath) as! Default_Custom_Cell
cell.lbl_Name.text = ""
return cell
}
// }
}
if allPermissionSection[2] == arraySectionName[2] {
// if indexPath.section == 3 {
if arrayTblItemsForEmergnceContact.count > 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: EMERGENCY_CONTACT_CELL, for: indexPath) as! EmergencyCnctCell
cell.updateWith(arrayTblItemsForEmergnceContact[indexPath.row], indexPath.row, arrayTblItemsForEmergnceContact.count, tblCellType)
return cell
}else{
let cell = tableView.dequeueReusableCell(withIdentifier: DEFAULT_CELL, for: indexPath) as! Default_Custom_Cell
cell.lbl_Name.text = ""
return cell
}
// }
}
return UITableViewCell()
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
let headerView = setupHeaderView(textHeading: " Details")
return headerView
}
if allPermissionSection[0] == arraySectionName[0] {
//if section == 1 {
let headerView = setupHeaderView(textHeading: "bers")
return headerView
// }
}
if allPermissionSection[1] == arraySectionName[1] {
// if section == 2 {
let headerView = setupHeaderView(textHeading: "sent")
return headerView
// }
}
if allPermissionSection[2] == arraySectionName[2] {
// if section == 3 {
let headerView = setupHeaderView(textHeading: "ontacts")
return headerView
// }
}
return UIView()
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 50
}
if allPermissionSection[0] == arraySectionName[0] {
// if section == 1 {
return 50
// }
}
if allPermissionSection[1] == arraySectionName[1] {
// if section == 2 {
return 50
// }
}
if allPermissionSection[1] == arraySectionName[1] {
// if section == 3 {
return 50
// }
}
return 0
}
setupHeaderView(textHeading:String) -> UIView? {
let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tblVw.frame.width, height: 50))
let headingLbl = UILabel()
let topLbl = UILabel()
let botomLbl = UILabel()
topLbl.translatesAutoresizingMaskIntoConstraints = false
botomLbl.translatesAutoresizingMaskIntoConstraints = false
headerView.addSubview(botomLbl)
headerView.addSubview(topLbl)
botomLbl.leadingAnchor.constraint(equalTo: headerView.leadingAnchor).isActive = true
botomLbl.trailingAnchor.constraint(equalTo: headerView.trailingAnchor).isActive = true
botomLbl.heightAnchor.constraint(equalToConstant:0.5).isActive = true
botomLbl.bottomAnchor.constraint(equalTo: headerView.bottomAnchor).isActive = true
topLbl.leadingAnchor.constraint(equalTo: headerView.leadingAnchor).isActive = true
topLbl.trailingAnchor.constraint(equalTo: headerView.trailingAnchor).isActive = true
topLbl.heightAnchor.constraint(equalToConstant:0.5).isActive = true
topLbl.topAnchor.constraint(equalTo: headerView.topAnchor).isActive = true
botomLbl.text = NULL_STRING
topLbl.text = NULL_STRING
topLbl.backgroundColor = .lightGray
botomLbl.backgroundColor = .lightGray
headingLbl.frame = CGRect.init(x: 0, y: 0, width: headerView.frame.width, height: headerView.frame.height)
headingLbl.text = textHeading
headingLbl.textColor = UIColor(hexString: "587358")
headerView.backgroundColor = UIColor(displayP3Red: 245/256, green: 245/256, blue: 245/256, alpha: 0.6)
headingLbl.textAlignment = .center
headerView.addSubview(headingLbl)
return headerView
}
So, Here is the my code please let me know if i m doing wrong please correct me
Can someone please explain to me hide or show randomly section,
Any help would be greatly appreciated.
Thanks in advance.

class AllDetailAccount {
var HeaderName: String?
var HeaderDeatil: [Any]?
init(HeaderName: String, HeaderDeatil: [Any]) {
self.HeaderName = HeaderName
self.HeaderDeatil = HeaderDeatil
}
}
class UnitUserDetail: AbstractController {
var allAcountDetailInfo = [AllDetailAccount]()
override func numberOfSections(in tableView: UITableView) -> Int {
return allAcountDetailInfo.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if allAcountDetailInfo[section].HeaderName == "USERDETAIL_STRING" {
return 1
}else if allAcountDetailInfo[section].HeaderName == "PHONE_NUMBR_STRING" {
let modelDate = allAcountDetailInfo[section].HeaderDeatil?[0] as! [PhoneNumber]
if modelDate.count == 0 {
return 1
}
return modelDate.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if allAcountDetailInfo[indexPath.section].HeaderName == "USERDETAIL_STRING" {
let modelDate = allAcountDetailInfo[indexPath.section].HeaderDeatil![0] as! UnitUserDetailModel
let cell = tableView.dequeueReusableCell(withIdentifier: "USER_DETAIL_CELL", for: indexPath) as! UserDetail_Cell
cell.updateWith(modelDate, indexPath.row, 0, tblCellType)
return cell
}else if allAcountDetailInfo[indexPath.section].HeaderName == "PHONE_NUMBR_STRING" {
let modelDate = allAcountDetailInfo[indexPath.section].HeaderDeatil?[0] as! [PhoneNumber]
let cell = tableView.dequeueReusableCell(withIdentifier: "PHONE_CELL", for: indexPath) as! PhoneNumberDetailCell
cell.updateWith(modelDate[indexPath.row], indexPath.row, modelDate.count, tblCellType)
return cell
}
let cell = tableView.dequeueReusableCell(withIdentifier: "DEFAULT_CELL", for: indexPath) as! Default_Custom_Cell
return cell
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 40))
view.backgroundColor = UIColor(displayP3Red: 245/256, green: 245/256, blue: 245/256, alpha: 0.6)
let lbl = UILabel(frame: CGRect(x: 15, y: 0, width: view.frame.width - 15, height: 40))
lbl.font = UIFont.systemFont(ofSize: 20)
lbl.text = allAcountDetailInfo[section].HeaderName
lbl.textAlignment = .center
lbl.textColor = UIColor(hexString: "HEADER_LABLE_TEXT_COLOUR")
let topLbl = UILabel()
view.addSubview(topLbl)
topLbl.translatesAutoresizingMaskIntoConstraints = false
topLbl.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
topLbl.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
topLbl.heightAnchor.constraint(equalToConstant:0.5).isActive = true
topLbl.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
topLbl.backgroundColor = .lightGray
topLbl.text = NULL_STRING
view.addSubview(lbl)
return view
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
}

Related

Ads array in tableview out of index

I have 3 arrays of data. I want to show ads after every 5 user cells in the table.
var nativeAds = [GADNativeAd]()
var item = [NewsElement]()
var filterItem = [NewsElement]()
After displaying 5 cells with ads, the application crashes with an error "Thread 1: Fatal error: Index out of range"
How do I re-add ads to the array or select an array with ads again?
There are smart guys sitting here, please help me. Thank you in advance!
below is my code
extension NewsViewController: GADNativeAdLoaderDelegate {
func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: Error) {
print("\(adLoader) failed with error: \(error.localizedDescription)")
}
func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADNativeAd) {
print("Received native ad: \(nativeAd)")
nativeAds.append(nativeAd)
}
func adLoaderDidFinishLoading(_ adLoader: GADAdLoader) {
newsTblView.reloadData()
}
}
extension NewsViewController: UITableViewDelegate, UITableViewDataSource {
private func dataRow(for indexPath: IndexPath) -> Int? {
let (quotient, remainder) = (indexPath.row + 1).quotientAndRemainder(dividingBy: numAdsToLoad)
if remainder == 0 { return nil }
return quotient * (numAdsToLoad - 1) + remainder - 1
}
private func adRow(for indexPath: IndexPath) -> Int? {
let (quotient, remainder) = (indexPath.row + 1).quotientAndRemainder(dividingBy: numAdsToLoad)
if remainder != 0 { return nil }
return quotient - 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchActive {
return filterItem.count
} else {
return item.count + nativeAds.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let row = dataRow(for: indexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "NewsTableViewCell", for: indexPath) as! NewsTableViewCell
if searchActive {
let items = filterItem[row]
cell.configCell(model: items )
} else {
let items = item[row]
cell.configCell(model: items )
}
cell.selectionStyle = .none
cell.backgroundColor = .clear
return cell
} else if let row = adRow(for: indexPath) {
let nativeAd = nativeAds[row] <-- "Thread 1: Fatal error: Index out of range"
nativeAd.rootViewController = self
let nativeAdCell = tableView.dequeueReusableCell(withIdentifier: "UnifiedNativeAdCell", for: indexPath) as! GADNativeAdViewCell
nativeAdCell.setAdData()
return nativeAdCell
}
fatalError("Did not find data or ad for cell: Should never get here")
}
}

Change label text, text color, text size according to tableView results

I have this code, where I have 2 Json arrays which I try to compare in my code -> all and allUrl. When array all contains some id from array allUrl image table row should be change to red, or vice versa green.
According this results I want to change label text, label text color, label text font size, but when I run app, label text still "PASSED" and gray, thanks.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
let listOfStudentsUrl = allUrl.reduce(into: [String:String]()){$0[$1.id] = $1.timestampValue}
// Compare data
listOfStudentsUrl.forEach { key in print(key)
if all[indexPath.row].id == key.key {
cell.textLabel?.text = all[indexPath.row].id
cell.detailTextLabel?.text = all[indexPath.row].timestampValue
cell.imageView!.image = UIImage(named:"red_icon")
cell.isHidden = false
statusLabel.text = "NOT PASSED"
statusLabel.textColor = UIColor.red
statusLabel.font = statusLabel.font.withSize(35)
break
} else {
statusLabel.text = "PASSED"
statusLabel.textColor = UIColor.gray
statusLabel.font = statusLabel.font.withSize(35)
cell.isHidden = true
}
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
var rowHeight:CGFloat = 0.0
let listOfStudentsUrl = allUrl.reduce(into: [String:String]()){$0[$1.id] = $1.timestampValue}
for key in listOfStudentsUrl{
if all[indexPath.row].id == key.key {
rowHeight = 49.0
break
} else {
rowHeight = 0.0
}
}
return rowHeight
}
Json format:
{
"class": {
"studentOne": {
"stringValue": "2020-02-04"
},
"studentTwo ": {
"stringValue": "2020-02-05"
}
},
"createTime": "2020-03-30",
"updateTime": "2020-03-30"
}

Fatal error: Array index out of range when appending array

I currently have an array which I am appending options to. They are they displayed in a table with 3 sections. The first 2 sections have 1 row each, but the third section has a variable number of rows depending on what is appended to the array. I essentially take the third component of my initial array (allAlbums[0].markscheme) and break it down to create multiple new items in the array.
However, when I am trying to simulate this, I get a fatal array on 'cell.textData?.text = section[indexPath.row] as! String' and I'm not sure why?
final class CaseViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var titleText: UILabel!
#IBOutlet var tableView: UITableView!
private var allAlbums = [Case]()
let kHeaderSectionTag: Int = 6900;
var expandedSectionHeaderNumber: Int = -1
var expandedSectionHeader: UITableViewHeaderFooterView!
var sectionItems: Array<Any> = []
var sectionNames: Array<Any> = []
var markschemeRows: Array<Any> = []
override func viewDidLoad() {
super.viewDidLoad()
allAlbums = LibraryAPI.shared.getCases()
// Filter the main array to match a single case
allAlbums = allAlbums.filter { $0.title == title}
// Get data to fill in to table
sectionNames = [ "Trainee Information", "Patient Information", "Examiner Information" ];
sectionItems = [ [allAlbums[0].doctor], [allAlbums[0].patient], [allAlbums[0].markscheme]]
let text = allAlbums[0].markscheme
markschemeRows = text.components(separatedBy: " ")
sectionItems.append(contentsOf: markschemeRows)
// Autoresize rows
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 500
// Remove excess row seperators
tableView.tableFooterView = UIView()
tableView.separatorColor = UIColor.clear
titleText.text = title
}
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.expandedSectionHeaderNumber == section) {
// Header section
let header = self.sectionNames[section] as! String
// If markscheme, create the markscheme format
if (header == "Examiner Information")
{
print(self.markschemeRows.count)
return self.markschemeRows.count
}
else
{
let arrayOfItems = self.sectionItems[section] as! NSArray
print(arrayOfItems.count)
return arrayOfItems.count
}
} else {
return 0;
}
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if (self.sectionNames.count != 0) {
return self.sectionNames[section] as? String
}
return ""
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44.0;
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat{
return 0;
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
//recast your view as a UITableViewHeaderFooterView
let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
header.contentView.backgroundColor = UIColor.darkGray
header.textLabel?.textColor = UIColor.white
if let viewWithTag = self.view.viewWithTag(kHeaderSectionTag + section) {
viewWithTag.removeFromSuperview()
}
let headerFrame = self.view.frame.size
let theImageView = UIImageView(frame: CGRect(x: headerFrame.width - 32, y: 13, width: 18, height: 18));
theImageView.image = UIImage(named: "Chevron-Dn-Wht")
theImageView.tag = kHeaderSectionTag + section
header.addSubview(theImageView)
// make headers touchable
header.tag = section
let headerTapGesture = UITapGestureRecognizer()
headerTapGesture.addTarget(self, action: #selector(CaseViewController.sectionHeaderWasTouched(_:)))
header.addGestureRecognizer(headerTapGesture)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! CustomTableCell
let section = self.sectionItems[indexPath.section] as! NSArray
cell.textLabel?.textColor = UIColor.black
cell.textData?.text = section[indexPath.row] as! String
return cell
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
// MARK: - Expand / Collapse Methods
#objc func sectionHeaderWasTouched(_ sender: UITapGestureRecognizer) {
let headerView = sender.view as! UITableViewHeaderFooterView
let section = headerView.tag
let eImageView = headerView.viewWithTag(kHeaderSectionTag + section) as? UIImageView
if (self.expandedSectionHeaderNumber == -1) {
self.expandedSectionHeaderNumber = section
tableViewExpandSection(section, imageView: eImageView!)
} else {
if (self.expandedSectionHeaderNumber == section) {
tableViewCollapeSection(section, imageView: eImageView!)
} else {
let cImageView = self.view.viewWithTag(kHeaderSectionTag + self.expandedSectionHeaderNumber) as? UIImageView
tableViewCollapeSection(self.expandedSectionHeaderNumber, imageView: cImageView!)
tableViewExpandSection(section, imageView: eImageView!)
}
}
}
func tableViewCollapeSection(_ section: Int, imageView: UIImageView) {
let sectionData = self.sectionItems[section] as! NSArray
self.expandedSectionHeaderNumber = -1;
if (sectionData.count == 0) {
return;
} else {
UIView.animate(withDuration: 0.4, animations: {
imageView.transform = CGAffineTransform(rotationAngle: (0.0 * CGFloat(Double.pi)) / 180.0)
})
var indexesPath = [IndexPath]()
for i in 0 ..< sectionData.count {
let index = IndexPath(row: i, section: section)
indexesPath.append(index)
}
self.tableView!.beginUpdates()
self.tableView!.deleteRows(at: indexesPath, with: UITableView.RowAnimation.fade)
self.tableView!.endUpdates()
}
}
func tableViewExpandSection(_ section: Int, imageView: UIImageView) {
let sectionData = self.sectionItems[section] as! NSArray
if (sectionData.count == 0) {
self.expandedSectionHeaderNumber = -1;
return;
} else {
UIView.animate(withDuration: 0.4, animations: {
imageView.transform = CGAffineTransform(rotationAngle: (180.0 * CGFloat(Double.pi)) / 180.0)
})
var indexesPath = [IndexPath]()
// Header section
let header = self.sectionNames[section] as! String
// If markscheme, create the markscheme format
if (header == "Examiner Information")
{
for i in 0 ..< markschemeRows.count {
let index = IndexPath(row: i, section: section)
indexesPath.append(index)
}
}
else
{
for i in 0 ..< sectionData.count {
let index = IndexPath(row: i, section: section)
indexesPath.append(index)
}
}
self.expandedSectionHeaderNumber = section
self.tableView!.beginUpdates()
self.tableView!.insertRows(at: indexesPath, with: UITableView.RowAnimation.fade)
self.tableView!.endUpdates()
}
}
}
The error is pretty clear.
In numberOfRows you return markschemeRows.count for section 2 which is the number of separated items in this line
markschemeRows = text.components(separatedBy: " ")
Then you must also get the item from markschemeRows rather than from section[indexPath.row] in cellForRow
var markschemeRows = [String]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! CustomTableCell
let section = self.sectionItems[indexPath.section] as! NSArray
cell.textLabel?.textColor = UIColor.black
if indexPath.section == 2 {
cell.textData?.text = markschemeRows[indexPath.row]
} else {
cell.textData?.text = section[indexPath.row] as! String
}
return cell
}
Your code is quite cumbersome. For example sectionNames and markschemeRows are clearly [String].Why do you declare the arrays as [Any]? This is Swift. Take care of the types. And don't use Foundation collection types like NSArray in Swift at all. Again take care of the types.

for loop only shows first item in array swift

I have a function called loadPosts that returns an array of Int values. Upon running it is used within the UITableView which has a function called setCell. Only the first item in the array is being used and then it repeats that value for the length of the array.
UPDATE 2:
Here are the parameters within the hhmessages array:
1. senderusername
2. recipient
3. message text
4. ava image
UPDATED: now includes additional code in loadPosts function
func loadPosts()->[Int] {
let me = user!["username"] as! String
let uuid = messages["uuid"] as! String
let url = URL(string: "http://localhost/message.php")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
let body = "username=\(me)&uuid=\(uuid)"
request.httpBody = body.data(using: String.Encoding.utf8)
URLSession.shared.dataTask(with: request) { data, response, error in
DispatchQueue.main.async(execute: {
if error == nil {
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
self.hhmessages.removeAll(keepingCapacity: false)
self.tableView.reloadData()
// declare new parseJSON to store json
guard let parseJSON = json else {
print("Error while parsing")
return
}
guard let messages = parseJSON["messages"] as? [AnyObject] else {
print("Error while parseJSONing")
return
}
self.hhmessages = messages
//print(self.hhmessages)
for i in 0 ..< self.hhmessages.count {
if me == self.hhmessages[i]["senderusername"]!! as! String {
self.incoming = [0]
}
if me == self.hhmessages[i]["recipient"]!! as! String {
self.incoming = [1]
}
}
self.tableView.reloadData()
return [Int()]
}
// UITableView
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ConversationCell
func setCell(incoming: [Int]) {
var layoutAttribute: NSLayoutAttribute
var layoutConstant: CGFloat
for i in 0 ..< self.incoming.count {
if (self.incoming[i] == 1) {
cell.bubbleImageView.image=#imageLiteral(resourceName: "chat_bubble_received")
cell.messageLbl.textColor = UIColor.black
layoutAttribute = .left
layoutConstant = 10
cell.contentView.addConstraint(NSLayoutConstraint(item: cell.bubbleImageView, attribute: layoutAttribute, relatedBy: .equal, toItem: cell.contentView, attribute: layoutAttribute, multiplier: 1, constant: layoutConstant))
}
if (self.incoming[i] == 0) {
cell.bubbleImageView.image = #imageLiteral(resourceName: "chat_bubble_sent")
cell.messageLbl.textColor = UIColor.white
layoutAttribute = .right
layoutConstant = -10
cell.contentView.addConstraint(NSLayoutConstraint(item: cell.bubbleImageView, attribute: layoutAttribute, relatedBy: .equal, toItem: cell.contentView, attribute: layoutAttribute, multiplier: 1, constant: layoutConstant))
}
}
}
// get main queue to this block of code to communicate back
DispatchQueue.main.async {
tableView.transform = CGAffineTransform(rotationAngle: -CGFloat.pi)
cell.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
setCell(incoming: self.incoming)
}
return cell
}
func loadPosts()->[Int] {
let me = user!["username"] as! String
let uuid = messages["uuid"] as! String
let url = URL(string: "http://localhost/message.php")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
let body = "username=\(me)&uuid=\(uuid)"
request.httpBody = body.data(using: String.Encoding.utf8)
URLSession.shared.dataTask(with: request) { data, response, error in
DispatchQueue.main.async(execute: {
if error == nil {
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
self.hhmessages.removeAll(keepingCapacity: false)
self.tableView.reloadData()
// declare new parseJSON to store json
guard let parseJSON = json else {
print("Error while parsing")
return
}
guard let messages = parseJSON["messages"] as? [AnyObject] else {
print("Error while parseJSONing")
return
}
self.hhmessages = messages
//print(self.hhmessages)
/// This is the part I edited
for i in 0 ..< self.hhmessages.count {
if me == self.hhmessages[i]["senderusername"]!! as! String {
self.incoming.append(0)
}
if me == self.hhmessages[i]["recipient"]!! as! String {
self.incoming.append(1)
}
}
self.tableView.reloadData()
return [Int()]
}
Change your cellForRowAt to use the indexPath.row as the index for self.incoming :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ConversationCell
func setCell(incoming: [Int]) {
var layoutAttribute: NSLayoutAttribute
var layoutConstant: CGFloat
if (self.incoming[indexPath.row] == 1) {
cell.bubbleImageView.image=#imageLiteral(resourceName: "chat_bubble_received")
cell.messageLbl.textColor = UIColor.black
layoutAttribute = .left
layoutConstant = 10
cell.contentView.addConstraint(NSLayoutConstraint(item: cell.bubbleImageView, attribute: layoutAttribute, relatedBy: .equal, toItem: cell.contentView, attribute: layoutAttribute, multiplier: 1, constant: layoutConstant))
}
if (self.incoming[indexPath.row] == 0) {
cell.bubbleImageView.image = #imageLiteral(resourceName: "chat_bubble_sent")
cell.messageLbl.textColor = UIColor.white
layoutAttribute = .right
layoutConstant = -10
cell.contentView.addConstraint(NSLayoutConstraint(item: cell.bubbleImageView, attribute: layoutAttribute, relatedBy: .equal, toItem: cell.contentView, attribute: layoutAttribute, multiplier: 1, constant: layoutConstant))
}
}
// get main queue to this block of code to communicate back
DispatchQueue.main.async {
tableView.transform = CGAffineTransform(rotationAngle: -CGFloat.pi)
cell.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
setCell(incoming: self.incoming)
}
return cell
}

Swift 3 JSON to array of objects is very slow

I am upgrading my app from swift 2 to swift 3 and have a significant performance loss when building an array of custom objects. In swift 2 this took a few seconds while in swift 3 it takes around 30 seconds. I am using alamofire to return swiftyJSON, returning about 3000 rows of data. the alamofire return is quick, its looping through this json to build array of custom objects thats slow. These objects greatly simplify the code written when building table cells and passing data to new views.
Code:
class Customer {
var ID: String!
var sysName: String!
var address: String!
var contactID: String!
required init(_name:String?, _id: String?, _address:String?, _contactID:String?) {
//print(json)
if _id != nil {
self.ID = _id
}else{
self.ID = ""
}
if _name != nil {
self.sysName = _name
}else{
self.sysName = ""
}
if _address != nil {
self.address = _address
}else{
self.address = "No Address on File"
}
if _contactID != nil {
self.contactID = _contactID
}else{
self.contactID = ""
}
}
}
Alamofire.request(API.Router.customerList()).responseJSON() {
response in
print(response.request ?? "") // original URL request
print(response.response ?? "") // URL response
print(response.data ?? "") // server data
print(response.result) // result of response serialization
if let json = response.result.value {
print("JSON: \(json)")
self.customers = JSON(json)
self.parseJSON()
}
}
func parseJSON(){
let jsonCount = self.customers["customers"].count
self.totalCustomers = jsonCount
for i in 0 ..< jsonCount {
self.loadedCustomers = i
print("customer = \(self.customers["customers"][i] ["sysName"].string!)")
//VERY SLOW
//create a customer object
let customer = Customer( _name: self.customers["customers"][i]["sysName"].string!, _id: self.customers["customers"][i]["ID"].string!, _address: self.customers["customers"][i]["mainAddr"].string!, _contactID: self.customers["customers"][i]["contactID"].string!)
//add customer to customer array
self.customersArray.append(customer)
}
self.layoutViews() //build view, call all table methods
}
Thanks
Improved code:
import Foundation
import UIKit
import Alamofire
import SwiftyJSON
enum SearchMode{
case name
case address
}
class CustomerListViewController: ViewControllerWithMenu, UITableViewDelegate, UITableViewDataSource, UISearchControllerDelegate, UISearchBarDelegate, UISearchDisplayDelegate, UISearchResultsUpdating{
var indicator: SDevIndicator!
var totalCustomers:Int!
//data arrays
var ids = [String]()
var names = [String]()
var addresses = [String]()
var searchController:UISearchController!
var currentSearchMode = SearchMode.name
var customerTableView:TableView = TableView()
var layoutVars:LayoutVars = LayoutVars()
var sections : [(index: Int, length :Int, title: String)] = Array()
var customersSearchResults:[String] = []
var shouldShowSearchResults:Bool = false
let viewsConstraint_V:NSArray = []
let viewsConstraint_V2:NSArray = []
override func viewDidLoad() {
super.viewDidLoad()
title = "Customer List"
view.backgroundColor = layoutVars.backgroundColor
getCustomerList()
}
func getCustomerList() {
//remove any added views (needed for table refresh
for view in self.view.subviews{
view.removeFromSuperview()
}
// Show Indicator
indicator = SDevIndicator.generate(self.view)!
Alamofire.request(API.Router.customerList()).responseJSON() {
response in
//print(response.request ?? "") // original URL request
//print(response.response ?? "") // URL response
//print(response.data ?? "") // server data
//print(response.result) // result of response serialization
do {
if let data = response.data,
let json = try JSONSerialization.jsonObject(with: data) as? [String: Any],
let customers = json["customers"] as? [[String: Any]] {
for customer in customers {
if let id = customer["ID"] as? String {
self.ids.append(id)
}
if let name = customer["sysName"] as? String {
self.names.append(name)
}
if let address = customer["mainAddr"] as? String {
self.addresses.append(address)
}
}
}
} catch {
print("Error deserializing JSON: \(error)")
}
// build sections based on first letter(json is already sorted alphabetically)
var index = 0;
var firstCharacterArray:[String] = [" "]
for i in 0 ..< self.names.count {
let stringToTest = self.names[i].uppercased()
let firstCharacter = String(stringToTest[stringToTest.startIndex])
if(i == 0){
firstCharacterArray.append(firstCharacter)
}
if !firstCharacterArray.contains(firstCharacter) {
let title = firstCharacterArray[firstCharacterArray.count - 1]
firstCharacterArray.append(firstCharacter)
let newSection = (index: index, length: i - index, title: title)
self.sections.append(newSection)
index = i;
}
if(i == self.names.count - 1){
let title = firstCharacterArray[firstCharacterArray.count - 1]
let newSection = (index: index, length: i - index, title: title)
self.sections.append(newSection)
}
}
self.layoutViews()
}
}
func layoutViews(){
indicator.dismissIndicator()
searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.placeholder = "Search Customers"
searchController.searchResultsUpdater = self
searchController.delegate = self
searchController.searchBar.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
navigationItem.titleView = searchController.searchBar
let items = ["Name","Address"]
let customSC = SegmentedControl(items: items)
customSC.selectedSegmentIndex = 0
customSC.addTarget(self, action: #selector(self.changeSearchOptions(sender:)), for: .valueChanged)
self.view.addSubview(customSC)
self.customerTableView.delegate = self
self.customerTableView.dataSource = self
self.customerTableView.register(CustomerTableViewCell.self, forCellReuseIdentifier: "cell")
self.view.addSubview(self.customerTableView)
//auto layout group
let viewsDictionary = [
"view2":customSC,
"view3":self.customerTableView
]as [String:AnyObject]
let sizeVals = ["fullWidth": layoutVars.fullWidth,"width": layoutVars.fullWidth - 30,"navBottom":layoutVars.navAndStatusBarHeight,"height": self.view.frame.size.height - 100] as [String:Any]
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view2(fullWidth)]", options: [], metrics: sizeVals, views: viewsDictionary))
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view3(fullWidth)]", options: [], metrics: sizeVals, views: viewsDictionary))
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-navBottom-[view2(40)][view3(height)]", options: [], metrics: sizeVals, views: viewsDictionary))
}
/////////////// Search Methods ///////////////////////
func changeSearchOptions(sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
currentSearchMode = .name
break
case 1:
currentSearchMode = .address
break
default:
currentSearchMode = .name
break
}
filterSearchResults()
}
func updateSearchResults(for searchController: UISearchController) {
filterSearchResults()
}
func filterSearchResults(){
customersSearchResults = []
switch currentSearchMode {
case .name:
self.customersSearchResults = self.names.filter({( aCustomer: String ) -> Bool in
return (aCustomer.lowercased().range(of: self.searchController.searchBar.text!.lowercased()) != nil) })
break
case .address:
self.customersSearchResults = self.addresses.filter({( aCustomer: String) -> Bool in
return (aCustomer.lowercased().range(of: self.searchController.searchBar.text!.lowercased()) != nil)
})
break
}
self.customerTableView.reloadData()
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
//print("searchBarTextDidBeginEditing")
shouldShowSearchResults = true
self.customerTableView.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
shouldShowSearchResults = false
self.customerTableView.reloadData()
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if !shouldShowSearchResults {
shouldShowSearchResults = true
self.customerTableView.reloadData()
}
searchController.searchBar.resignFirstResponder()
}
/////////////// TableView Delegate Methods ///////////////////////
func numberOfSections(in tableView: UITableView) -> Int {
if shouldShowSearchResults{
return 1
}else{
return sections.count
}
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
//print("titleForHeaderInSection")
if shouldShowSearchResults{
return nil
}else{
if(sections[section].title == "#"){
return " # \(self.totalCustomers) Customers Found"
}else{
return " " + sections[section].title //hack way of indenting section text
}
}
}
func sectionIndexTitles(for tableView: UITableView) -> [String]?{
print("sectionIndexTitlesForTableView 1")
if shouldShowSearchResults{
return nil
}else{
//print("sectionIndexTitlesForTableView \(sections.map { $0.title })")
return sections.map { $0.title }
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
//print("heightForHeaderInSection")
if shouldShowSearchResults{
return 0
}else{
return 50
}
}
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
return index
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//print("numberOfRowsInSection")
if shouldShowSearchResults{
return self.customersSearchResults.count
} else {
return sections[section].length
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = customerTableView.dequeueReusableCell(withIdentifier: "cell") as! CustomerTableViewCell
customerTableView.rowHeight = 50.0
if shouldShowSearchResults{
let searchString = self.searchController.searchBar.text!.lowercased()
if(currentSearchMode == .name){
cell.nameLbl.text = self.customersSearchResults[indexPath.row]
cell.name = self.customersSearchResults[indexPath.row]
if let i = self.names.index(of: cell.nameLbl.text!) {
//print("\(cell.nameLbl.text!) is at index \(i)")
cell.addressLbl.text = self.addresses[i]
cell.address = self.addresses[i]
cell.id = self.ids[i]
} else {
cell.addressLbl.text = ""
cell.address = ""
cell.id = ""
}
//text highlighting
let baseString:NSString = cell.name as NSString
let highlightedText = NSMutableAttributedString(string: cell.name)
var error: NSError?
let regex: NSRegularExpression?
do {
regex = try NSRegularExpression(pattern: searchString, options: .caseInsensitive)
} catch let error1 as NSError {
error = error1
regex = nil
}
if let regexError = error {
print("Oh no! \(regexError)")
} else {
for match in (regex?.matches(in: baseString as String, options: NSRegularExpression.MatchingOptions(), range: NSRange(location: 0, length: baseString.length)))! as [NSTextCheckingResult] {
highlightedText.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellow, range: match.range)
}
}
cell.nameLbl.attributedText = highlightedText
}else{//address search mode
cell.addressLbl.text = self.customersSearchResults[indexPath.row]
cell.address = self.customersSearchResults[indexPath.row]
if let i = self.addresses.index(of: cell.addressLbl.text!) {
cell.nameLbl.text = self.names[i]
cell.name = self.names[i]
cell.id = self.ids[i]
} else {
cell.nameLbl.text = ""
cell.name = ""
cell.id = ""
}
//text highlighting
let baseString:NSString = cell.address as NSString
let highlightedText = NSMutableAttributedString(string: cell.address)
var error: NSError?
let regex: NSRegularExpression?
do {
regex = try NSRegularExpression(pattern: searchString, options: .caseInsensitive)
} catch let error1 as NSError {
error = error1
regex = nil
}
if let regexError = error {
print("Oh no! \(regexError)")
} else {
for match in (regex?.matches(in: baseString as String, options: NSRegularExpression.MatchingOptions(), range: NSRange(location: 0, length: baseString.length)))! as [NSTextCheckingResult] {
highlightedText.addAttribute(NSBackgroundColorAttributeName, value: UIColor.yellow, range: match.range)
}
}
cell.addressLbl.attributedText = highlightedText
}
} else {
//print("make cell")
cell.id = self.ids[sections[indexPath.section].index + indexPath.row]
cell.name = self.names[sections[indexPath.section].index + indexPath.row]
cell.address = self.addresses[sections[indexPath.section].index + indexPath.row]
cell.nameLbl.text = self.names[sections[indexPath.section].index + indexPath.row]
cell.addressLbl.text = self.addresses[sections[indexPath.section].index + indexPath.row]
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexPath = tableView.indexPathForSelectedRow;
let currentCell = tableView.cellForRow(at: indexPath!) as! CustomerTableViewCell
let customerViewController = CustomerViewController(_customerID: currentCell.id)
navigationController?.pushViewController(customerViewController, animated: false )
tableView.deselectRow(at: indexPath!, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Resources