tableview loading invisible rows as well , cellforrowat index path getting celled for hidden rows - swift

Tableview loading invisible rows as well , cellForRowAtIndexPath getting celled for hidden rows
func numberOfSections(in tableView: UITableView) -> Int {
return ordersArray.count;
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let selectedOrder = ordersArray[indexPath.section]
return CGFloat(180 + (30 * selectedOrder.lineItems().count ));
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(indexPath.row == 1)
{
let deliveryShipChargesCustomCell =
tableView.dequeueReusableCell(withIdentifier: "DeliveryShipChargesCustomCell", for: indexPath) as! DeliveryShipChargesCustomCell
return deliveryShipChargesCustomCell;
}
else
{
let orderCustomCell = tableView.dequeueReusableCell(withIdentifier: "OrdersCustomCell" , for: indexPath) as! OrdersCustomCell
return orderCustomCell;
}
}
problem with this code snippet is that it gets load row that are not even visible for example if order array count is 10. and there are two rows in each section , initially tableview (_ tableView: UITableView, cellForRowAtIndexPath: IndexPath) gets called for 20 times.
Any thoughts?

Related

tableview header and prototype cell showing wrong

Is there are reason why there are height differences between what is shown and what has been set as a height? also there is a space between header and prototype cell which I cannot get rid of.
extension FirstTabThirdView: UITableViewDataSource , UITableViewDelegate {
func tableView (_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = headerView
return view
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellOne", for: indexPath)
cell.textLabel?.text = "hello"
return cell
}
}

How do I auto-display length in Table View?

I can't determine the length of Cell TableView automatically
in Swift ,UIKit
I tried all the ways
Either it gives me a fixed length or the values in the Cell tableview are not shown
These are Table View codes
extension DetilesVC : UITableViewDelegate , UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrQ.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellDetiles
let date = arrQ[indexPath.row]
cell.arrQR = date
return cell
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
}
If I don't put this value here like this
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 900
}
Table View shouts like this
My Cell TableView
var arrQR : QRModel? {
didSet {
SetupQR()
}
}
func SetupQR () {
qusLabel.text = arrQR?.sub
detailLabel.text = arrQR?.detiles
}
In cellForRowAt put following code to show values in cells -
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellDetiles
let date = arrQ[indexPath.row]
cell.textLabel?.text = date
return cell
}

Header view not showing at Section 0

I add custom tableView header. My section count is 2 and the header is not displaying at section 0. But showing properly at section 1.
func numberOfSections(in tableView: UITableView) -> Int {
return arrRouteDetials.count
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
headerRadioBtn.tag = section
headerRadioBtn.addTarget(self, action:#selector(RdAction(_sender: )), for: .touchUpInside)
return viewTblHeader
}else{
headerRadioBtn.tag = section
headerRadioBtn.addTarget(self, action:#selector(RdAction(_sender: )), for: .touchUpInside)
return viewTblHeader
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrOfSectionRowValue[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BranchUnloadingcell", for: indexPath) as! BranchUnloadingcell
cell.lblSerialNo.text = "\(indexPath.row + 1)"
cell.val = arrOfSectionRowValue[indexPath.section][indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
self.viewWillLayoutSubviews()
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 74
}

Scroll to bottom TableView with custom section and cell Swift

My tableview has custom sections and cells. Every time the user scroll to the bottom it will call the server and load additional datas. My problem is how do I detect the last section so it could load more data? Each section contain 1 to 3 rows. My willDisplay wasn't working properly and the refreshControl.beginRefreshing() indicator didn't show too. Thanks!
let refreshControl = UIRefreshControl()
//MARK - Header Sections
func numberOfSections(in tableView: UITableView) -> Int {
return tableData[segmentedControlIndex].count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as! HomeSectionTableViewCell
let model = events[segmentedControlIndex][section]
cell.setup(model: model)
.
.
.
return cell.contentView
}
//MARK - Cells
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData[segmentedControlIndex][section].rows.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "detailCell") as! HomeTableViewDetailCell
.
.
.
cell.detailTitleLabel?.text = tableData[segmentedControlIndex][indexPath.section].rows[indexPath.row].detailTitleLabelString
cell.detailSubtitleLabel?.text = tableData[segmentedControlIndex][indexPath.section].rows[indexPath.row].detailSubtitleLabelString
return cell
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.000001
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell: HomeTableViewDetailCell = tableView.cellForRow(at: indexPath) as? HomeTableViewDetailCell {
cell.detailTitleLabel?.numberOfLines = cell.detailTitleLabel?.numberOfLines == 0 ? 1 : 0
tableView.reloadData()
}
}
//MARK - TableView add more data
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.section == tableView.numberOfSections - 1 {
self.refreshControl.beginRefreshing()
self.page = self.page + 1
self.getMoreData() { result in
self.refreshControl.endRefreshing()
print("Load More Bets Data reload: \(result == true ? "Success":"Failure")")
}
}
}

Hide Cell when the condition is met

Hello StackOverflowers!
I am trying to hide cells when condition is met - in this case when the user survey comment is empty. The data is loaded from Firebase This is what I have tried so far: hiding the cell as well as selecting it. Then follow with heightForRow for selectedRows. But it's not working. Please help! Thank you~~~
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return comment.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CommentsTableCell", for: indexPath) as! CommentsCell
let surveyCommentList = self.comment[indexPath.row]
cell.selectionStyle = UITableViewCell.SelectionStyle.none
if surveyCommentList.surveyComment == "" {
cell.isHidden = true
cell.isSelected = true
} else {
cell.surveyCommentText.text = surveyCommentList.surveyComment
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if let selectedRows = tableView.indexPathsForSelectedRows, selectedRows.contains(indexPath) {
return 0
} else {
return 50
}
}
This is the correct implementation :
// Define a new filtered array from your comments that filters empty Comments
let filteredComments = comment.filter({!$0.surveyComment.isEmpty)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredComments.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CommentsTableCell", for: indexPath) as! CommentsCell
let surveyCommentList = self.filteredComments[indexPath.row]
cell.selectionStyle = UITableViewCell.SelectionStyle.none
cell.surveyCommentText.text = surveyCommentList.surveyComment
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}