Insert individual custom table view cell with a time interval of 3 seconds.
Add a data for the custom cell to the dataSource of your table view every 3 seconds.When a button is clicked, custom table view cells will start to appear within the table view by 3 seconds.
If you could show your current code, showing something in code gets easier. Can you show your code?
func prepareAsDestination( segue: UIStoryboardSegue, sender: Any?){
let sourceForC = segue.source as! SourceForC
sourceForC.prepare(for: segue, from: sender, toC: self)
}
}
extension liveViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 5
}
func tableView( tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return namesArray.count
}
func tableView( tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell : DynamicTableViewCell? = tableView.dequeueReusableCell(withIdentifier: "DynamicTableViewCell") as? DynamicTableViewCell
if cell == nil {
cell = Bundle.main.loadNibNamed("DynamicTableViewCell", owner: self, options: nil)?.first as? DynamicTableViewCell
}
cell?.userNameLabel.text = namesArray[indexPath.row]
return cell!
}
}
By the way, you marked this thread as SOLVED. Does that mean you have solved your issue by yourself?
If not, please read the followings.
DynamicTableViewCell is the custom cell class you want to add, right?
The contents shown in the custom cell is held in namesArray, of type [String]?
Please show the action method called When a button is clicked.
You have 5 sections in your table view, each of them shows the same contents, why?
One more, you should better use the Code block feature available in the editor area of this site, indicated as < >.
No I have not solved the issue, I clicked that by accident.
Yes DynamicTableViewCell is the custom cell class I want to add.
The contents are held in "namesArray" of type [String] for now, but Im trying to figure this out for the UILabel https://developer.apple.com/forums/thread/669218
Code Block @IBAction func startLivePressed(_ sender: UIButton) { self.userName = userNameTextField.text! startLiveButtonIsSelected = !startLiveButtonIsSelected performSegue(withIdentifier: "segueToCamera", sender: self) }
//this the method that prepares the segue.
Code Block func prepare(for segue: UIStoryboardSegue, from sender: Any?, toC vc: liveViewController) { vc.finalUserName = self.userName vc.finalProfilePic = imageView.image }
//The tableView is just being displayed within the "liveViewController". There is no data being passed from the "ViewController" for it
I have 5 sections because I wanted to see if id be able to scroll up and down through the table(Im new to tableViews).
This is the first time you mentioned ViewController.The tableView is just being displayed within the "liveViewController". There is no data being passed from the "ViewController" for it
We readers have no info about your project. Please explain the first thing.
The button is in ViewController and not in liveViewController, right?
How your namesArray is initialized in liveViewController?
If liveViewController is presented using segue, without knowing the initial state, the word add does not make sense.
Yes, the button is in the ViewController
namesArray is initialized in the liveViewController
What other info do you need to help solve this issue? And also thank you for your help.
The dev forums is not relevant here. If you could care about all readers who do not know your project, that would be fine.I am new to Swift and apple developer forums.
Thanks for clarifying. But I want a little more.the button is in the ViewController
The button causes the liveViewController shown, and inside the newly shown liveViewController,
custom table view cells will start to appear within the table view by 3 seconds. OK?
Sorry, I was not clear enough. With How I wanted to know the initial contents of the Array. Is it empty?namesArray is initialized in the liveViewController
Showing your code would be clearer than explaining with hundreds of words.
Code Block import UIKit import AVFoundation protocol SourceForC{ func prepare(for segue: UIStoryboardSegue, from sender: Any?, toC vc: liveViewController) } class liveViewController: UIViewController, UINavigationControllerDelegate { @IBOutlet weak var tableView: UITableView! let namesArray = ["Theresa:", "Carson:", "Renaldo:", "Aaron:", "Henry:", "Jacob:", "Luke:", "Nathan:" , "Noah:" ] override func viewDidLoad() { super.viewDidLoad() tableView.estimatedRowHeight = 45 tableView.rowHeight = UITableView.automaticDimension } extension liveViewController : SegueDestination { func prepareAsDestination( segue: UIStoryboardSegue, sender: Any?){ let sourceForC = segue.source as! SourceForC sourceForC.prepare(for: segue, from: sender, toC: self) } } extension liveViewController: UITableViewDelegate, UITableViewDataSource { func numberOfSections(in tableView: UITableView) -> Int { return 5 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return namesArray.count } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableView.automaticDimension } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cell : DynamicTableViewCell? = tableView.dequeueReusableCell(withIdentifier: "DynamicTableViewCell") as? DynamicTableViewCell if cell == nil { cell = Bundle.main.loadNibNamed("DynamicTableViewCell", owner: self, options: nil)?.first as? DynamicTableViewCell } cell?.userNameLabel.text = namesArray[indexPath.row] return cell! } }
The button causes the liveViewController shown, and inside the newly shown liveViewController,
custom table view cells will start to appear within the table view by 3 seconds. OK?
Yes the button cause the liveViewController to be shown. And inside the newly shown liveViewController, custom tableView cells should start to appear within the table view every 3 seconds.
//This is the relevant code within the ViewController
Code Block class ViewController: UIViewController, UINavigationControllerDelegate, SourceForC { @IBAction func startLivePressed(_ sender: UIButton) { startLiveButtonIsSelected = !startLiveButtonIsSelected performSegue(withIdentifier: "segueToCamera", sender: self) } func prepare(for segue: UIStoryboardSegue, from sender: Any?, toC vc: liveViewController) { } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { segue.prepare(sender:sender) } }
//And if this is needed, this is all the relevant code within the DynamicTableViewCell.swift. This was the file that was made automatically when I made the DynamicTableViewCell.xib
Code Block import UIKit class DynamicTableViewCell: UITableViewCell { @IBOutlet weak var userNameLabel: UILabel! @IBOutlet weak var commentLabel: UILabel! override open func awakeFromNib() { } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } }
Unfortunately, your code shown causes some errors and I guess you have omitted some relevant extensions.
With filling some parts by guess, I got this:
liveViewController:
Code Block class liveViewController: UIViewController, UINavigationControllerDelegate { @IBOutlet weak var tableView: UITableView! //let namesArray = ["Theresa:", "Carson:", "Renaldo:", "Aaron:", "Henry:", "Jacob:", "Luke:", "Nathan:" , "Noah:" ] var cellContentsArray: [CellContent] = [ CellContent(name: "Theresa:", randomString: nil), CellContent(name: "Carson:", randomString: nil), CellContent(name: "Renaldo:", randomString: nil), CellContent(name: "Aaron:", randomString: nil), CellContent(name: "Henry:", randomString: nil), CellContent(name: "Jacob:", randomString: nil), CellContent(name: "Luke:", randomString: nil), CellContent(name: "Nathan:", randomString: nil), CellContent(name: "Noah:", randomString: nil), ] override func viewDidLoad() { super.viewDidLoad() tableView.estimatedRowHeight = 45 tableView.rowHeight = UITableView.automaticDimension } private var timer: Timer? = nil /// Call this method in the main thread func startAddingRandomStringCell() { timer?.invalidate() timer = Timer.scheduledTimer(withTimeInterval: 3.0, repeats: true) {_ in var newCellContent = CellContent() //Create a new CellContent with random string newCellContent.name = nil newCellContent.randomString = UUID().uuidString self.cellContentsArray.append(newCellContent) self.tableView.reloadData() // `tableView.insertRows(at:with:)` may be preferred } } func stopAddingRandomStringCell() { timer?.invalidate() timer = nil } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) startAddingRandomStringCell() } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) stopAddingRandomStringCell() } }
Code Block extension liveViewController: UITableViewDelegate, UITableViewDataSource { func numberOfSections(in tableView: UITableView) -> Int { return 5 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { //return namesArray.count return cellContentsArray.count } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableView.automaticDimension } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cell : DynamicTableViewCell? = tableView.dequeueReusableCell(withIdentifier: "DynamicTableViewCell") as? DynamicTableViewCell if cell == nil { cell = Bundle.main.loadNibNamed("DynamicTableViewCell", owner: self, options: nil)?.first as? DynamicTableViewCell } //cell?.userNameLabel.text = namesArray[indexPath.row] cell?.userNameLabel.text = cellContentsArray[indexPath.row].name //`commentLabel` is what you want to show the random string in? cell?.commentLabel.text = cellContentsArray[indexPath.row].randomString return cell! } }
CellContent.swift:
Code Block import Foundation struct CellContent { var name: String? var randomString: String? }
Please try.
//When all of the names and comments have been ran through it starts to fill the labels with random numbers and letters. Is there a way to randomize what name and comment is being used in each cell to prevent it from ever running out?
Code Block var cellContentsArray: [CellContent] = [ CellContent(name: "Theresa:", randomString: "test1"), CellContent(name: "Carson:", randomString: "test2"), CellContent(name: "Renaldo:", randomString: "test3"), CellContent(name: "Aaron:", randomString: "test4"), CellContent(name: "Henry:", randomString: "test5"), CellContent(name: "Jacob:", randomString: "test5"), CellContent(name: "Luke:", randomString: "test6"), CellContent(name: "Nathan:", randomString: "test7"), CellContent(name: "Noah:", randomString: "test8"), ]
Also is there a way to automatically scroll to the bottom of the table view when a new cell is added? Right now you have to scroll down manually every time.
Thank you!
Sorry, but I do not understand what you want to do. With having limited number of names, you will eventually run out of names.When all of the names and comments have been ran through it starts to fill the labels with random numbers and letters. Is there a way to randomize what name and comment is being used in each cell to prevent it from ever running out?
Actually, there is. But before applying that, you need to stop showing 5 sections.Also is there a way to automatically scroll to the bottom of the table view when a new cell is added? Right now you have to scroll down manually every time.
With showing the same things in 5 sections, you cannot tell which one is the last one.