UITableView background colour for bottom 5 rows - ios

I do know how to input background colours for my row, but I don't really know how I can filter it by only the bottom 5 rows are "cell.backgroundColor = UIColor.red;" whereas the rest stays the same. Appreciate those who can help me this thanks!
P.S: Sorry as my swift is quite rusty.
UITableView Controller
import UIKit
import FirebaseDatabase
var postData2 = [String]()
var postData3 = [String]()
var tableDataArray = [tableData]()
class ResultsController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference() //set the firebase reference
// Retrieve the post and listen for changes
databaseHandle = ref?.child("Posts3").observe(.value, with: { (snapshot) in
postData2.removeAll()
postData3.removeAll()
tableDataArray.removeAll()
for child in snapshot.children {
let snap = child as! DataSnapshot
let key = snap.key
let value = String(describing: snap.value!)
let rating = (value as NSString).integerValue
postData2.append(key)
postData3.append(value)
tableDataArray.append(tableData(boothName: key, boothRating: rating))
}
postData2.removeAll()
postData3.removeAll()
let sortedTableData = tableDataArray.sorted(by: { $0.boothRating > $1.boothRating })
for data in sortedTableData {
postData2.append(data.boothName)
let value = String(describing: data.boothRating)
postData3.append(value)
}
self.tableView.reloadData()
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postData2.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.font = UIFont.init(name: "Helvetica", size: 23)
cell.textLabel?.text = postData2[indexPath.row]
cell.detailTextLabel?.text = postData3[indexPath.row] + " ♥"
cell.detailTextLabel?.textColor = UIColor.red;
cell.detailTextLabel?.font = UIFont.init(name: "Helvetica", size: 23)
// cell.backgroundColor = UIColor.red;
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 80
}
}
class tableData {
var boothName: String
var boothRating: Int
init(boothName: String, boothRating: Int) {
self.boothName = boothName
self.boothRating = boothRating
}
}

A simple way is to have an conditional check to see if the indexPath.row value is within the last five.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if(indexPath.row >= postData2.count-5){
cell.backgroundColor = UIColor.red
}else{
cell.backgroundColor = UIColor.white /* Remaining cells */
}
return cell
}

Some of the other answers will work - but it is nicer to use cells that have a known configuration when they are dequeued by cellForRowAt, not deal with a bunch of possible starting conditions each time you dequeue a cell. To do this subclass the UITableViewCell and override prepareForReuse(). This function will be called just before a cell is returned by dequeueReusableCell. Then cells can be set to a known starting point before you configure them. If cells could be received configured any possible way in cellForRowAt, you soon wind up with a very long function with a lot of if/else conditions.
The condition
if indexPath.row >= postData2.count - 5 {
cell.backgroundColor = UIColor.red
}
can be used as it is, and prepareForReuse takes care of the cells not keeping any settings when they are recycled. Here's an example:
override func prepareForReuse() {
super.prepareForReuse()
backgroundColor = UIColor.white
}
With this one simple setting it's a wash whether you do the if/else approach or use subclassing to make the most of prepareForReuse. But as soon as you have more than one thing to set in a cell you will find it is far less complex to use this function and results in far fewer mistakes with the appearance of cells - consider what would happen if there were more than one possible color a cell could be, or there were multiple elements in the cell to be configured with multiple possible values...

You can add simple logic
if indexPath.row >=(postData2.count-5) {
cell.backgroundColor = UIColor.red
}else {
cell.backgroundColor = UIColor.white
}

Just check a condition for setting the red colour for last five rows.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if(indexPath.row >= postData2.count-5){
cell.backgroundColor = UIColor.red;
}else {
cell.backgroundColor = UIColor.white; //white colour for other rows
}
return cell
}

This method is recommended by the system, this method is more circumventing reuse in some cases (like when you modify the contents of a control in the cell surface)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "cell")
// Not the type of cell, if the queue will return nil, at this time requires create ⼀ cell
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
}
}
If it involves data processing, you can create a new NSMutableSet(), Used to store your operations (ordinary data is lost, stored in the didSelecetRow inside indexPath like) save anyway, a unique tag.
These are just solve the problem of multiplexing, to deal with discoloration, refer to the above solution.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if(indexPath.row >= postData2.count-5){
cell.backgroundColor = UIColor.red
}else{
cell.backgroundColor = UIColor.white /* Remaining cells */
}
return cell
}

Related

Only first two sender.tag is working for section header in table view

I have a table view inside which I am calling multiple nib as row under section
// MARK:- EXTENSIONS TABLE VIEWS
*
extension HomeController: UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func numberOfSections(in tableView: UITableView) -> Int {
return 9
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
let headerCell = tableView.dequeueReusableCell(withIdentifier: TableCellConstants.hHeaderCell) as! HomeHeaderCell
headerCell.viewAllBtn.tag = section
headerCell.viewAllBtn.addTarget(self, action: #selector(self.viewAll), for: .touchUpInside)
headerView.addSubview(headerCell)
return headerView
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 4{
guard let cell = self.productTableView.dequeueReusableCell(withIdentifier: TableCellConstants.trendingProductCell, for: indexPath) as? TrendingCell else{
return UITableViewCell()
}
return cell
}
else if indexPath.section == 5{
guard let cell = self.productTableView.dequeueReusableCell(withIdentifier: TableCellConstants.featureCell, for: indexPath) as? FeatureBrandCell else{
return UITableViewCell()
}
return cell
}else if indexPath.section == 6{
guard let cell = self.productTableView.dequeueReusableCell(withIdentifier: TableCellConstants.spotlightCell, for: indexPath) as? StoplightTableCell else{
return UITableViewCell()
}
return cell
}
guard let cell = self.productTableView.dequeueReusableCell(withIdentifier: TableCellConstants.momentCell, for: indexPath) as? PriviewProductCell else{
return UITableViewCell()
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 4{
return 310
}
else if indexPath.section == 5{
return 200
}
else if indexPath.section == 6{
return 320
}
else{
return 210
}
}
}
this is button action
#objc func viewAll(sender: UIButton){
print(sender.tag)
}
when I click on view all Button only first two section is working that means output is only 0 and 1 tags, remaining section button action not even working, I even put breakpoint nothing is coming on it, not only this even horizontal scroll is not working on collection view that I have under table view which
is under scroll view
I'm am not sure to what you did wrong in your code, however I created a test project to try and sort your issue out and I believe I have managed to do so.
My best guess at where you went wrong was either something to do with the UIButton's connection being made from interface builder to code. Or to do with how you specified which cells should be at which row and in which section, inside of the cellForRowAt method (I'll look at your code in more detail and update this if I find why your code was not working).
Heres the code in its entirety which worked for me:
View Controller:
NOTE: I changed the if statement blocks to a switch statement as I
thought it would make it easier to read, also as I did not have the
xib files you were using for the cells I created my own basic header
cell, I then used the basic preset table view cell for the cells
contained within each section. If you have not worked with switch
statements before, then I recommend you check out the section on them
in the swift language guide book:
https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID127
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet var tableView: UITableView!
// MARK:: Life-Cycle
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(HeaderTableViewCell.nib(), forCellReuseIdentifier: HeaderTableViewCell.id)
}
// MARK: Button Actions
#objc func viewAll(sender: UIButton){
print(sender.tag)
}
// MARK: Data Source
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func numberOfSections(in tableView: UITableView) -> Int {
return 9
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
let headerCell = tableView.dequeueReusableCell(withIdentifier: HeaderTableViewCell.id) as! HeaderTableViewCell
headerCell.viewAllBtn.tag = section
headerCell.viewAllBtn.addTarget(self, action: #selector(viewAll), for: .touchUpInside)
headerView.addSubview(headerCell)
return headerView
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 4:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "Section 4"
return cell
case 5:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "Section 5"
return cell
case 6:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "Section 6"
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "Unknown Section (Default)"
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath.section {
case 4:
return 310
case 5:
return 200
case 6:
return 320
default:
return 210
}
}
}
HeaderTableViewCell Cell xib
HeaderTableViewCell Code
class HeaderTableViewCell: UITableViewCell {
#IBOutlet var headerTitleLabel: UILabel!
#IBOutlet var viewAllBtn: UIButton!
static let id = "HeaderTableViewCell"
static func nib() -> UINib {
return UINib(nibName: "HeaderTableViewCell", bundle: nil)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
NOTE: Please also be aware that it probably would be a better idea for you to of just used a custom UIView instead of a custom UITableViewCell for your table view headers.
As i said I had collection view as row inside table view and that table view inside scroll view, so at last I removed scroll view from main view and kept all UI under table view now it working fine
Swift compiler get confused when we have three scrolling view working together so better avoid this, it is not best approach
Thanks #demented07 for your efforts

Swift UitableViewcell with button

I have a strange behavior in the UITABLEVIEW.
I have a TableView with Button, what I wanted to do is when I clicked to a button in the tableView, I want the color border to change to red.
The problem is that the color is changing not only for the clicked button, but also for others row in the tableview:
Here is my implementation
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! chooseProductTVC
cell.Btn_AddProduct.addTarget(self, action: #selector(self.btnAction(_:)), for: .touchUpInside)
return cell
}
#objc func btnAction(_ sender: MyButton) {
sender.BorderColor = UIColor.red
let point = sender.convert(CGPoint.zero, to: tbl_View_ChooseProduct as UIView)
let indexPath: IndexPath! = self.tbl_View_ChooseProduct.indexPathForRow(at: point)
let object = self.fetchedResultsController.object(at: indexPath)
print (object.produit_name)
print("row is = \(indexPath.row) && section is = \(indexPath.section)")
}
As you can see in the picture below I have only clicked on the first button (Abricot) ==> other button has also automatically changed the border (Avocat) and many others.
This is because of cell dequeuing try to re set when you load the table , suppose here default is blue
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
cell.Btn_AddProduct.addTarget(self, action: #selector(self.btnAction(_:)), for: .touchUpInside)
if selected
cell.Btn_AddProduct.BorderColor = UIColor.red
else
cell.Btn_AddProduct.BorderColor = UIColor.blue
return cell
}
Since we reuse the cell for displaying, we have to modify the color every time after dequeing.
Add new property selected to product model.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! chooseProductTVC
cell.Btn_AddProduct.addTarget(self, action: #selector(self.btnAction(_:)), for: .touchUpInside)
let object = self.resultArray(at: indexPath.row)
if object.selected
cell.Btn_AddProduct.BorderColor = .red
else
cell.Btn_AddProduct.BorderColor = .blue
return cell
}
#objc func btnAction(_ sender: MyButton) {
sender.BorderColor = UIColor.red
let point = sender.convert(CGPoint.zero, to: tbl_View_ChooseProduct as UIView)
let indexPath: IndexPath! = self.tbl_View_ChooseProduct.indexPathForRow(at: point)
let object = self.fetchedResultsController.object(at: indexPath)
object.selected = true
print (object.produit_name)
print("row is = \(indexPath.row) && section is = \(indexPath.section)")
}
Tip: You could use tableChooseProduct(iOS) instead of tbl_View_ChooseProduct.(android). This link may helpful.
You have to store default Colors in Dictionary.
var orderColor = [Int : UIColor]()
override func viewDidLoad() {
super.viewDidLoad()
for i in 0...29 // TotaL number of rows
{
orderColor[i] = UIColor.blue // DEFAULT color of the Button
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 30
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! chooseProductTVC
cell.Btn_AddProduct.addTarget(self, action: #selector(self.btnAction(_:)), for: .touchUpInside)
cell.Btn_AddProduct.BorderColor = orderColor[indexPath.row]
cell.selectionStyle = .none
return cell
}
#objc func btnAction(_ sender: MyButton) {
let point = sender.convert(CGPoint.zero, to: tbl_View_ChooseProduct as UIView)
let indexPath: IndexPath! = self.tbl_View_ChooseProduct.indexPathForRow(at: point)
orderColor[indexPath.row] = UIColor.red
tbl_View_ChooseProduct.reloadData()
}

UITableView not updating on reloadData

I have two tableview on the screen. I have set delegate & datasource for both tableview. Lets have consider one table to show main filters & on click of particular cell/filter i have to reload subfilters in second tableview.
So i tried very simple solutions, didSelectRowAt,
subfilterTableView.reloadData()
Even i have tried by calling on main thread too,
DispatchQueue.main.async {
subfilterTableView.reloadData()
}
Still its not reloading the subfilterTableView.
I know this beginUpdates() & endUpdates() method are only for to insert & delete cells still i have reload in between beginUpdates() & endUpdates() it make crash as it is accepted.
I know this is stupid question but i have tried every possible simpler solutions.
Following are the some conditions which i come across:
Sometime data get populated on second click
Sometime data get populated after 3-5 seconds
Data get populated on refresh of tableview too
irony is Data get properly populated on real device
Following is my code:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView.tag == 1 {
return filters.count
}
else{
return subFilters.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView.tag == 1 {
let filter = filters[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "FilterTableViewCell", for: indexPath) as! FilterTableViewCell
cell.labelFilter.text = filter["filterName"] as? String
cell.backgroundColor = UIColor.clear
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
cell.selectionStyle = .none
return cell
}
else{
let cell = tableView.dequeueReusableCell(withIdentifier: "SubFilterTableViewCell", for: indexPath) as! SubFilterTableViewCell
cell.labelSubfilter.text = subFilters[indexPath.row]
cell.backgroundColor = UIColor.clear
cell.selectionStyle = .none
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.tag == 1 {
let selectedCell:FilterTableViewCell = tableView.cellForRow(at: indexPath)! as! FilterTableViewCell
selectedCell.labelFilter.textColor = UIColor.black
selectedCell.contentView.backgroundColor = UIColor.white
subFilters = filters[indexPath.row]["subFilterValue"] as! [String]
self.tableViewSubfilters.reloadData()
// DispatchQueue.main.async {
// self.tableViewSubfilters.reloadData()
// }
// tableViewSubfilters.performSelector(onMainThread: #selector(self.reloadData), with: nil, waitUntilDone: true)
}
}
It seems that your code does not have any issue
Just run it on real device instead of simulator
Do check the Datasource and Delegates of your second tableview.
subfilterTableView.dataSource = self
subfilterTableView.delegate = self
Put in viewDidLoad():
func viewDidLoad() {
tableView.delegate = self
tableView.datasource = self
}

Adding UIButton to detailTextLabel/right side of UITableView

I'm quite new to Swift and I would like to know what is the easiest and simplest way for me to add in 10 buttons to 10 of my cell rows in my TableViewController.
P.S: It would be nice if the 10 buttons perform differently instead of duplicate.
import UIKit
import FirebaseDatabase
var ref: DatabaseReference?
var databaseHandle: DatabaseHandle?
var postData = [String]()
class TableViewController5: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference() //set the firebase reference
// Retrieve the post and listen for changes
databaseHandle = ref?.child("Posts3").observe(.value, with: { (snapshot) in
postData.removeAll()
for child in snapshot.children {
let snap = child as! DataSnapshot
let key = snap.key
postData.append(key)
}
self.tableView.reloadData()
})
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.font = UIFont.init(name: "Helvetica", size: 23)
cell.textLabel?.text = postData[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 80
}
}
From what I can understand, you should use prototype cell and add an IBAction in the cell and in that cell you should perform the segue and pass whatever data you need to customise the page you load.
In your tableView(_:cellForRowAt:) method, you can set a tag for the button.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? prototypeCell
cell.voteButton.tag = indexPath.row
return cell
}
Then in your cell's class, in the IBAction I mentioned earlier, check for the tag and set the data to pass accordingly.
Then in your prepareForSegue:sender: method, just pass the data you want to pass to the next view controller and all should work fine.
Check if it helps You
//Common cell Variable
var cell = UITableViewCell()
//UIButton commonly Declared
var acceptRequest = UIButton()
var declineRequest = UIButton()
My TableView Cell contains two button as shown Accept & Decline
In my tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) I added Handlers for button
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//Cell for dequeuing
cell = self.RequestListTableView.dequeueReusableCell(withIdentifier: "Cell")!
//Used Tags here
//You can Make use of TableViewCell class here and Access Buttons from there
acceptRequest = cell.viewWithTag(4) as! UIButton
//Adding action Handlers for UIbuttons
self.acceptRequest.addTarget(self, action: #selector(RequestListView.AcceptRequest(sender:)), for: .touchUpInside)
return cell
}
my Button handler
func AcceptRequest (sender: UIButton) {
//Dequeue cell with identifier can be ignored
cell = self.RequestListTableView.dequeueReusableCell(withIdentifier: "Cell")!
//Get button position from TableView : Required
let buttonPosition : CGPoint = sender.convert(CGPoint.zero, to: self.RequestListTableView)
//Get index Path of selected Cell from TableView
//Returns Table Cell index same as didSelect
let indexPath : IndexPath = self.RequestListTableView.indexPathForRow(at: buttonPosition)!
//Can be used if you need to update a view
cell = self.RequestListTableView.cellForRow(at: indexPath)!
//Now time to access using index Path
cell.textLabel.text = array[indexPath.row] //Example
}
Can use this Procedure for even single Button or for multiple buttons too
In case if you have more than one button in one cell, this will help you

Why is the top tableViewCell gray?

I am working with a UITableView in a normal UIViewController. For some reason, the text of the top cell in my TableView is always colored gray.
I have been trying to figure out what causes that cell to be gray, but don't know what it is. The data in my tableView is sourced from an array called fileList that gets refreshed during both viewWillAppear and viewDidAppear.
Why is the top cell always gray with this code?
(And it does happen both with nothing in the fileList and with many things in the fileList)
//MARK: - TableView
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if fileList.count == 0 {
return 1
} else {
return fileList.count
}
}
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if fileList.count == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.isUserInteractionEnabled = false
cell.accessoryType = UITableViewCellAccessoryType.none
cell.textLabel?.text = "No documents found"
cell.textLabel?.textAlignment = .center
cell.textLabel?.textColor = UIColor.black
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.isUserInteractionEnabled = true
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
cell.textLabel?.text = (fileList[indexPath.row])
cell.textLabel?.textAlignment = .left
cell.textLabel?.textColor = UIColor.black
return cell
}
}
}
There is no issue running your code , It's running fine for me.
Even setting cell.selectionstyle = .grey is not putting grey color to cell or text.
You have to be more clear about question .
1) What is grey? text color or cell color.
2) What is the code that you are hiding or doing else apart?
extension ViewController : UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return fileList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
let rowData = fileList[indexPath.row]
cell?.textLabel?.text = rowData
//cell?.isSelected = true
cell?.selectionStyle = .gray
cell?.isUserInteractionEnabled = true
cell?.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
cell?.textLabel?.textAlignment = .left
cell?.textLabel?.textColor = UIColor.black // if let priceString = rowData[2] as? String { cell.priceLabel.text = "Price: $(priceString)" }
return cell!
}
// Number of sections in table
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}// Default is 1 if not implemented
}
I believe this is the same as the problem mentioned here. Try setting:
cell?.textLabel?.isEnabled = true`
in the if statement block for fileList.count == 0 after the line
cell.isUserInteractionEnabled = false

Resources