How do I auto-display length in Table View? - swift

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
}

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

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
}

How do I move custom cells to left in editing mode in UITableView

I have custom cells of UITableView which consist of UIImage and textLabel. When I select the editing mod, the textLabel shows the following image;
I want to move it to left. I tried all of things to do but it didn't work.
Please let me know.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allRows.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "editCell", for: indexPath) as! CustomTableView
cell.textLabel?.text = self.allRows[indexPath.row].bname
cell.textLabel?.textAlignment = .left
let image:UIImage = UIImage(named: "group")!
cell.imageView!.image = image
return cell
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle{
return .delete
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath){
if editingStyle == .delete{
self.allRows.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath as IndexPath], with: .fade)
}
}
#objc func leftEditButtonPressed(_ sender: UIButton) {
self.tableView!.isEditing = true
self.tableView?.setEditing(true, animated: true)
}
look at this, I hope it helps you:
swipe left/right in tableViewCell
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let closeAction = UIContextualAction(style: .normal, title: "Close", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("OK, marked as Closed")
success(true)
})
closeAction.image = UIImage(named: "tick")
closeAction.backgroundColor = .purple
return UISwipeActionsConfiguration(actions: [closeAction])
}
Try using the following code:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataArr.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80.0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableCell") as? TestTableCell else {
return UITableViewCell()
}
cell.customImageView.image = #imageLiteral(resourceName: "ic_audio")
cell.customTitleLabel.text = self.dataArr[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.delete
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
print("delete at \(indexPath.row)")
}
}
Here is the output for this code:
Note: You can change the editing action in editingStyleForRowAt method and for performing the action you need to write the logic in commit editingStyle method.
Hope this help you!
I suggest you to create new label in that UITableViewCell and connect that label to custom UITableViewCell class ! then you are free to do anything !
Swift 4 :
Create a new class which is subclass of UITableViewCell !
In your storyboard in right menu which called Utilities in toolbar of Utilities go to the Identity inspector defince your cell class to your new UITableViewCell class
Drag and drop UILabel from Object library into your cell
now using drag and drop connect that label to your UITableViewCell class then create an Outlet
Perfect !
now use myLabel instead of textLabel ;)
dont forget to cast your cell as! YourCustomCellClass
Good Luck ;)

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

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?

can't use my function for viewdidload Use of unresolved identifier 'getSegue' swift

cant call func for viewdidload
CODE
Use of unresolved identifier 'getSegue'
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! konuCell
func getSegue() {
cell.dersAdiLabel.text = gelenDersAdi
}
return cell
}
Basically, You written your function in the cellForRowAtIndexPath this will call nested Function but you have to write this at class scope.
In short writegetSegue() function at class level scope just like that:
func getSegue() {
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! konuCell
cell.dersAdiLabel.text = gelenDersAdi
return cell
}
To access getSegue, move it outside the scope of tableView(_:cellForRowAt:), e.g.:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
[...]
}
func getSegue() {
[...]
}
...but since getSegue would require a cell that it can modify, this is not going to work the way you want. I'd suggest keeping the code in cellForRowAt and reload the table view in viewDidLoad.