How to set multiple UITableview in single ViewController - swift

I'm trying to make two TableViews in one ViewController and
having a trouble in cellForRowAt indexPath
I tried var cell:UITableviewCell? , return cell!
and now let cell = UITableViewCell() but it's still not working
#IBOutlet weak var ButtonA: UIButton!
#IBOutlet weak var ATV: UITableView!
#IBOutlet weak var ButtonB: UIButton!
#IBOutlet weak var BTV: UITableView!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
if tableView == self.ATV {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = Alist[indexPath.row]
let bgColorView = UIView()
bgColorView.backgroundColor = UIColor(red:0.93, green:0.60, blue:0.49, alpha:0.5)
cell.selectedBackgroundView = bgColorView
}
if tableView == self.BTV {
let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath)
cell1.textLabel?.text = Blist[indexPath.row]}
return cell
}
I want each button to have a different list

Related

Swift UITableViewCell not showing Labels

I'm trying to create a custom cell view with some labels, I have created a subclass of UITableViewCell and connected the labels to it. Inside cellForRowAt method I dequeued the cell and cast it as YourCell subclass to access the labels and set text.
Setting class of cell to YourCell
However when running the app, nothing is showing up.
class YourCell: UITableViewCell {
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var label2: UILabel!
#IBOutlet weak var label3: UILabel!
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
let array1 = ["Name1", "Name2","Name3","Name3"]
let array2 = ["1","2","3","4"]
let array3 = ["18","17","11","9"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.delegate = self
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! YourCell
cell.label1.text = array1[indexPath.row]
cell.label2.text = array2[indexPath.row]
cell.label3.text = array3[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array2.count
}
}
You have conformed the UITableViewDelegate protocol correctly, the only thing you are missing is to set up your datasource by including this line in your viewDidLoad.
tableview.dataSource = self

Swift How should Custom Cell load tableview data and make it expand when I use didSelectRowAt?

I create a custom cell that include tableview
Here's the MainTableView and data
var data = [People(name:"Kevin",age:"18",tall:"180")]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainCell", for: indexPath) as! MainTableViewCell
cell.title = data[indexPath.row].name
cell.detail = ["age \(data[indexPath.row].age)","tall \(data[indexPath.row].tall)"]
cell.isExtend = false
return cell
}
I try to tap cell to expand tableView height and load data
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainCell", for: indexPath) as! MainTableViewCell
cell.isExpand = !cell.isExpand
}
Here's the MainTableViewCell
class MainTableViewCell: UITableViewCell {
#IBOutlet weak var title: UILabel!
#IBOutlet weak var detailTableView: UITableView!
#IBOutlet weak var detailTableViewHeight: NSLayoutConstraint!
var detail:[String] = []{
didSet{
detailTableView.reloadData()
}
}
var isExpand: Bool = false{
didSet{
detailTableView.isHidden = !isExpand
detailTableView.reloadData()
detailTableViewHeight.constant = isExpand ? detailTableView.contentSize.height:0
}
}
override func awakeFromNib() {
super.awakeFromNib()
detailTableView.delegate = self
detailTableView.dataSource = self
detailTableView.isScrollEnabled = false
detailTableViewHeight.constant = 0
}
}
I use tableView to load data and hide some of theme first, and tapped to show and hide theme
but It happened nothing
Did I forgot something?
In cellforRowAt:
let cell: MoreUserDetails = tableView.dequeueReusableCell(withIdentifier: "MoreUserDetails") as! MoreUserDetails
cell.backgroundColor = .clear
cell.selectionStyle = .none
if isExpand {
// your func when its expanded
}
else {
// your func when its hidden
}
return cell
in did select
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
if isSelected[indexPath.row] {
isExpand[indexPath.row] = false
}
else {
isExpand[indexPath.row] = true
}
self.tableVIew.reloadData()
}

Multiple custom cell in TableViewController

Scenario
| First Cell |
| Second Cell |
| SWITCH |
| |
| Third Cell |
| TextField |
I created a dynamic table view with three different classes: SecondTableCell (inside it has the outlet of a switch) and ThirdTableCell (inside it has an outlet of a textField)
The switch is Off.
I need to see textField.isUserInteractionEnabled = false then the switch is turned on.
How can I do this?
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var customTableView: UITableView!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellSwitch = tableView.dequeueReusableCell(withIdentifier: "cellSwitch")! as! SwitchTableViewCell
let cellText = tableView.dequeueReusableCell(withIdentifier: "cellText")! as! TextFieldTableViewCell
}
class SwitchTableViewCell: UITableViewCell {
#IBOutlet weak var switchOutlet: UISwitch!
#IBAction func switchAction(_ sender: UISwitch) {
if sender.isOn == true{
print("swithOn")
}else{
print("swithOff")
}
}
}
class TextFieldTableViewCell: UITableViewCell {
#IBOutlet weak var textFieldColor: UITextField!
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SwitchDelegate {
#IBOutlet weak var customTableView: UITableView!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellSwitch = tableView.dequeueReusableCell(withIdentifier: "cellSwitch")! as! SwitchTableViewCell
let cellText = tableView.dequeueReusableCell(withIdentifier: "cellText")! as! TextFieldTableViewCell
cellSwitch.delegate = self
cellText.delegate = self
}
func valueDidChange(isOn: Bool) {
tableView.visibleCells.forEach { cell
if let cell = cell as? TextFieldTableViewCell {
cell.textField.isUserInteractionEnabled = false
}
}
}
}
protocol SwitchDelegate {
func valueDidChange(isOn: Bool)
}
class SwitchTableViewCell: UITableViewCell {
#IBOutlet weak var switchOutlet: UISwitch!
var delegate: SwitchDelegate?
#IBAction func switchAction(_ sender: UISwitch) {
delegate?.valueDidChange(isOn: sender.isOn)
}
}
class TextFieldTableViewCell: UITableViewCell {
#IBOutlet weak var textField: UITextField!
}
When you setup the cells in the tableviewcontroller, you can define at view row you want to use which tableviewcell class. Just like in my below example:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if ((indexPath.row < 25 && counter > 25) || (indexPath.row < counter)) {
let cell = tableView.dequeueReusableCell(withIdentifier: "SaleItems", for: indexPath) as! MyTableCellTableViewCell
let TapRecognize = UITapGestureRecognizer(target: self, action: #selector(self.PictureTap(sender:)))
TapRecognize.numberOfTapsRequired = 1
TapRecognize.name = jSonData[indexPath.row]["advid"]
// Configure the cell...
cell.Megnevezes.text=jSonData[indexPath.row]["advdescription"]!
cell.EladasiAr.text=jSonData[indexPath.row]["advprice"]! + " " + jSonData[indexPath.row]["advpricecur"]!
cell.addGestureRecognizer(TapRecognize)
cell.LoadPicture(AdvID: Int(jSonData[indexPath.row]["advid"]!)!)
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "PagerCell", for: indexPath) as! ChangePageCell
cell.delegate = self
return cell
}
}
In my example the last row of the tableview is different. It is not exactly clear from your explanation what do you really want to do with the switch cell. But you should ensure that you can identify the cells and their classes to know where to pass the action's.
In my case the gestureRecognizer gets a name, which contains a relevant advertisement ID to load the photos, linked to that advertisement details.
When you tap on the cell, it calls the tap recogniser's action and name can be used to identify which cell is touched.

UIlabel text alignment in UITableViewCell not working

Sorry for the Chinese characters, but the UILabel text alignment displayed as right side NO MATTER which option (left, center and etc.) it is set. Please help.
// This is the function that populates the cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! WordLibraryTableViewCell
let key = Int(dataSource[indexPath.section][indexPath.row])
let value = levelDict[key!]! as String
cell.wordLibLabel.text = value
return cell
}
// This is the cell class
class WordLibraryTableViewCell: UITableViewCell {
#IBOutlet weak var bookimageView: UIImageView!
#IBOutlet weak var wordLibLabel: UILabel!
}

Swift, preventing tableview cell resueable

How to solve the table view data overlapped issue?
I have searched some Object C version answer, but it seems not working for Swift. I tried to get cell==nil, but it gives me an error
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let current_patient = patients[indexPath.row]
let cell = myTable.cellForRowAtIndexPath(indexPath) as! PatientTableViewCell
if (cell){
let cell = myTable.dequeueReusableCellWithIdentifier("patientCell") as! PatientTableViewCell
}
let cell = myTable.dequeueReusableCellWithIdentifier("patientCell", forIndexPath: indexPath) as! PatientTableViewCell
if(cell != nil){
var subviews = cell.contentView.subviews
subviews.removeAll()
}
//configure cell
let type = current_patient.enrollable_type
cell.patientTypelbl.text = type
return cell
}
After two-days struggling, I figure it out finally. The key to solve it is to find which cell is been resued. What I am doing now is give a var identifier to cell.
class PatientTableViewCell: UITableViewCell {
#IBOutlet weak var followupBtn: UIButton!
#IBOutlet weak var viewBtn: UIButton!
#IBOutlet weak var titleType: UILabel!
#IBOutlet weak var titleID: UILabel!
#IBOutlet weak var patientTypelbl: UILabel!
#IBOutlet weak var patientIDlbl: UILabel!
**var identifier = false**
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let current_patient = patients[indexPath.row]
var cell = myTable.dequeueReusableCellWithIdentifier("patientCell") as! PatientTableViewCell
if(cell.identifier == true){
cell = myTable.dequeueReusableCellWithIdentifier("patientCell") as! PatientTableViewCell
}
//config cell
cell.identifier = true //important
I hope it can help someone else. :)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let current_patient = patients[indexPath.row]
let cell: PatientTableViewCell! = tableView.dequeueReusableCellWithIdentifier("patientCell") as? PatientTableViewCell
//configure cell
let type = current_patient.enrollable_type
cell.patientTypelbl.text = type
return cell
}