How to use TableViewCell when using TableView on Swift

Asked 2 years ago, Updated 2 years ago, 110 views

Please tell me how to use multiple TableView Cells when using TableView on Swift.

As an image, I would like to display two different cells in one table.
As for the atmosphere, I would like to display the first cell several times, and when a certain time comes, I would like to display the second cell additionally, the first cell, and finally the second cell.

Using UITable View, we placed two UITable View Cells in StoryBord.The Table View Cell Identifiers for the cells we placed were tableCell and tableCell2, respectively.Label was placed on tableCell and Text Field was placed on tableCell2.
StoryBord

import UIKit

classViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{

@IBOutlet variable:UITableView!

let testString: NSAray=["Sunday", "Monday", "Tuesday" ]
let testString2: NSArray=["Wednesday", "Thursday", "Friday", "Saturday" ]

override func viewDidLoad(){
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from anib.
}

// Specify the number of cells in Table View
functableView(_table:UITableView, numberOfRowsInSection:Int) - > Int{

    return testString.count
}

// Configure elements for each cell
functableView(_table:UITableView, cellForRowAtindexPath:IndexPath) - >UITableViewCell{
    varcellName —String

     cellName="tableCell"

    letcell=table.dueReusableCell(withIdentifier:cellName, for:indexPath)

    letlabel=table.viewWithTag(1)as!UILabel

    label.text="\"(testString [indexPath.row])"

    return cell


}


override funcdidReceiveMemoryWarning(){
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

I was able to write the code as above and display the contents of the testString.
However, I have tried many things, but I don't know how to display tableCell2 continuously.In addition, I was not able to display tableCell2 after displaying tableCell2 and display tableCell2 again.
I would appreciate it if you could teach me how to write about that area.
Thank you for your cooperation.

swift swift3 uitableview

2022-09-30 19:47

1 Answers

For the time being, it would be good to sort the cell types by tableView(_tableView:UITableView, cellForRowAtindexPath:IndexPath).

It looks like this.

class ViewController:UIViewController, UITableViewDataSource{

    @IBOutlet weak variableView:UITableView!

    let testString = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]

    override func viewDidLoad(){
        super.viewDidLoad()
        self.tableView.dataSource=self
    }

    // MARK: -UITableViewDataSource

    func numberOfSections (tableView: UITableView) - > Int {
        return1
    }

    functableView(_tableView:UITableView, numberOfRowsInSection section:Int) - > Int{
        return self.testString.count
    }

    functableView(_tableView:UITableView, cellForRowAtindexPath:IndexPath) - >UITableViewCell{
        if indexPath.row<3||indexPath.row>5{
            letcell=tableView.dueReusableCell(withIdentifier: "tableCell")!
            letlabel=cell.viewWithTag(1)as!UILabel
            label.text="\"(self.testString [indexPath.row])"
            return cell
        }
        letcell=tableView.dueReusableCell(withIdentifier: "tableCell2")!
        letfield=cell.viewWithTag(2)as!UITextField
        field.text="\" (self.testString [indexPath.row])"
        return cell       
    }


}

Ideally, if the cell shape is complex or more diverse, create a subclass of UITableViewCell and perform the configuration process within that subclass.
tableView(_tableView:UITableView, cellForRowAtindexPath:IndexPath) selects which subclass to instantiate.

Also, if you find it complicated to choose which string to display, you may want to choose from the current indexPath.
If it becomes more complicated, such as taking it from the Internet, it is better to separate classes.


2022-09-30 19:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.