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.
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.
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.
568 Who developed the "avformat-59.dll" that comes with FFmpeg?
886 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
567 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
599 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.