I am trying to have two tableviews in a single view, i have given a unique identifier for each cell and each tableview has their own cell class. The codes are
class ReportsViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var tableView2: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
extension ReportsViewController : UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! BreakingNewsTableViewCell
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 145
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Breaking News"
}
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .medium
let date = Date()
let dateString = dateFormatter.string(from: date)
return "Updated on " + dateString
}
private func tableView2(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
private func tableView2(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath) as! TopStoriesTableViewCell
return cell
}
private func tableView2(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 145
}
private func tableView2(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
private func tableView2(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return " Top Stories"
}
private func tableView2(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .medium
let date = Date()
let dateString = dateFormatter.string(from: date)
return "Updated on " + dateString
}
}
However, it keeps crashing as says my cell isnt registered? May i know what mistake i did here? I have also linked the tableview delegates to self
You need to switch all methodfs like this
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == self.tableView {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! BreakingNewsTableViewCell
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath) as! cell2
return cell
}
}
Related
My certain two rows of data of UITableViewCell exchange, and this is how it looks.
The row data after exchanged are the right, and they're wrong before exchanged.
And I'm sorry for not have enough reputation to post .gif image.
Is there any way to avoid the data exchange to the wrong row?
my-data-changes-in-uitableviewcell-when-i-scroll-down-and-get-back
ios-uitableview-mixes-up-data-when-scrolling-too-fast
swift-user-input-mixed-up-when-uitableview-reuses-cells
swift-2-tableview-scrolling-changes-data
The links of problems above seem to be related to mine, but I think my problem is not caused by scrolling.
I setup UITableViewCell with DispatchQueue.global(qos: .background).async, and this function is inside the cell itself.
--SensorRecordTableViewCell (UITableViewCell)--
func getNewLiveInfo(shedId: String, sensorCategory: String) {
DispatchQueue.global(qos: .background).async {
let id = self.shedId.cString(using: .utf8)
let shed_id = UnsafeMutablePointer(mutating: id)
let category = self.sensorCategory.cString(using: .utf8)
let sensor_category = UnsafeMutablePointer(mutating: category)
if let data = getliveInfo(shed_id, sensor_category) {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
// record_id, sensor_id, sensor_category, sensor_value, read_time
self.sensorRecord.recordId = String(cString: data.pointee!)
self.sensorRecord.sensorId = String(cString: (data+1).pointee!)
self.sensorRecord.sensorCategory = String(cString: (data+2).pointee!)
self.sensorRecord.value = Double(String(cString: (data+3).pointee!))
self.sensorRecord.time = formatter.date(from: String(cString: (data+4).pointee!))
DispatchQueue.main.async {
self.setValueAndTime()
}
data.deallocate()
}
}
}
And call the function above from func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
--UITableViewDelegate, UITableViewDataSource--
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
var cell = cell as! SensorRecordTableViewCell
cell.getNewLiveInfo(shedId: shed.id!, sensorCategory: config.sensorRecordOrder[indexPath.row])
}
Finally, I setup the cell from func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: sensorCellId, for: indexPath) as! SensorRecordTableViewCell
cell.setUpView(shedId: shed.id!, sensorCategory: config.sensorRecordOrder[indexPath.row])
return cell
}
Is there any way to avoid the data exchange to the wrong row?
Here is a simple example to show what it should looks like,
hope it helps.
class SensorRecord {
var recordID: Int = 0
var sensorID: Int = 0
}
class ViewController: UIViewController {
private var dataSource = [SensorRecord]()
override func viewDidLoad() {
super.viewDidLoad()
// request infos from your server
getNewLiveInfo(completion: { (sensorRecords)
// after the request success, reload data
self.dataSource = sensorRecords
self.tableView.reloadData()
})
}
}
// MARK: UITableViewDataSource
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ID", for: indexPath)
// let your cell to show datas from your server
let sensorRecord = dataSource[indexPath.row]
cell.update(with: sensorRecord)
return cell
}
}
I have multiple section and each section can have multiple rows.
Code : Display as excepted.
class SampleViewController: UIViewController {
let sectionArray = ["pizza", "deep dish pizza", "calzone"]
let items = [["Margarita", "BBQ Chicken", "Peproni"], ["Margarita", "meat lovers", "veggie lovers"], ["sausage", "chicken pesto", "BBQ Chicken"]]
#IBOutlet weak var listObj: UITableView!
var selectedItems = [String]()
override func viewDidLoad() {
super.viewDidLoad()
registerCell()
// Do any additional setup after loading the view.
}
func registerCell(){
self.listObj.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
}
extension SampleViewController : UITableViewDelegate,UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return sectionArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = items[indexPath.section][indexPath.row]
if selectedItems.contains(items[indexPath.section][indexPath.row]) {
print("Selected Item")
cell.accessoryType = .checkmark
} else {
print("Item not selected")
cell.accessoryType = .none
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 44
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionArray[section].uppercased()
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedItems.append(items[indexPath.section][indexPath.row])
tableView.reloadData()
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
selectedItems.removeAll { $0 == items[indexPath.section][indexPath.row] }
tableView.reloadData()
}
}
Here I want to select row in a section, assume that Pizza section contains Margarita row and deep dish pizza as well contains same value. Here we need to select both rows which are different sections. It has to match with other section has same row or not whenever user tap on rows if match, all row has to select.
Store selected item names in an array and reload the tableview. In cellForRowAt method check if the array has the current item or not.
var selectedItems = [String]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = items[indexPath.section][indexPath.row]
if selectedItems.contains(items[indexPath.section][indexPath.row]) {
print("Selected Item")
} else {
print("Item not selected")
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if selectedItems.contains(items[indexPath.section][indexPath.row]) {
print("Selected Item")
selectedItems.removeAll { $0 == items[indexPath.section][indexPath.row]
} else {
print("Item not selected")
selectedItems.append(items[indexPath.section][indexPath.row])
}
tableView.reloadData()
}
class InfotableViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
let myActivityIndicator = UIActivityIndicatorView()
var name = [String]()
var imagearray=[UIImage]()
#IBOutlet weak var table: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 150
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCell(withIdentifier: "cell") as! profileTableViewCell
cell.tableimage.image = nil;
cell.tableimage.image = imagearray[indexPath.row] as! UIImage
cell.tablelabel.text = name[indexPath.row] as! String
self.myActivityIndicator.stopAnimating()
return cell
}
You should do this,
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
and this,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCell(withIdentifier: "cell") as! profileTableViewCell
cell.tableimage.image = nil;
let index = indexPath.row%5
cell.tableimage.image = imagearray[index] as! UIImage
cell.tablelabel.text = name[indexPath.row] as! String
self.myActivityIndicator.stopAnimating()
return cell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1")
cell?.textLabel?.text = (list1[indexPath.row % list1.count]
cell?.imageView?.image = (list1[indexPath.row % list1.count]
return cell!
}
After following a guide on how to add collectionViews inside a tableViewCell (here) and then I tried to see if I can switch these two cells.
However, when I try to scroll down the collectionview(horizontally) I get an error that the collectionview index goes out of bounds. I tried to see if adding a reload data for each collection view would help with that issue, but it doesn't... Code below:
class BuildTravelPackage: UIViewController, UITableViewDataSource,UITableViewDelegate, UICollectionViewDelegate {
#IBOutlet var optionsTableView: UITableView!
var items = [[PackageOption]]()
override func viewDidLoad() {
super.viewDidLoad()
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.longPress(longPressGestureRecognizer:)))
self.optionsTableView.addGestureRecognizer(longPressRecognizer)
//load data into cells
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.alpha = 0
UIView.animate(withDuration: 1.0) {
cell.alpha = 1.0
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 175
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .none
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let item = items[sourceIndexPath.row]
items.remove(at: sourceIndexPath.row)
items.insert(item, at: destinationIndexPath.row)
optionsTableView.isEditing = !optionsTableView.isEditing
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = optionsTableView.dequeueReusableCell(withIdentifier: "optionCell", for: indexPath) as! PackageOptionTableViewCell
cell.collectionView.delegate = self
cell.collectionView.dataSource = self
cell.collectionView.tag = indexPath.row
cell.collectionView.reloadData()
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Column:",indexPath.row)
print("Row:",collectionView.tag)
//highlght selected cell and load a new cell with infomation? Or load a uiview with date selection
}
func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.began {
let touchPoint = longPressGestureRecognizer.location(in: self.view)
if let indexPath = optionsTableView.indexPathForRow(at: touchPoint) {
optionsTableView.isEditing = !optionsTableView.isEditing
}
}
}
}
extension BuildTravelPackage:UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items[collectionView.tag].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "singleOptionCell", for: indexPath) as! OptionCollectionViewCell
cell.image.image = items[collectionView.tag][indexPath.row].imageFile
cell.label.text = items[collectionView.tag][indexPath.row].name
return cell
}
}
I think problem is here when you are replacing objects
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let item = items[sourceIndexPath.row]
items.remove(at: sourceIndexPath.row)
items.insert(item, at: destinationIndexPath.row)
optionsTableView.isEditing = !optionsTableView.isEditing
}
try replacing code by
swap(& items[sourceIndexPath.row], & items[destinationIndexPath.row])
Im really new to Swift, the question is how can I represent values from array in label.
I want a TableView with cells dynamically represent values from array into the labels which will be created in tableView rows.
import UIKit
import Foundation
class TableViewMarketItemsViewCell: UITableViewController {
var fruits = ["Avocado", "Apricot", "Pomegranate", "Quince"]
var PriceArray = ["1000 тг.","4000 тг.","3000 тг.","2000 тг."]
var categoryArray = ["Green category","Maroon category","Red category","Yellow category"]
// MARK: - UITableViewDataSource
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fruits.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) as! TableViewCell
let fruitName = fruits[indexPath.row]
cell.productTitle.text = fruitName
cell.productImage.image = UIImage(named: fruitName)
return cell
}
}
Thnx in advance
import UIKit
import Foundation
class TableViewMarketItemsViewCell: UITableViewController {
var fruits = ["Avocado", "Apricot", "Pomegranate", "Quince"]
var PriceArray = ["1000 тг.","4000 тг.","3000 тг.","2000 тг."]
var categoryArray = ["Green category","Maroon category","Red category","Yellow category"]
// MARK: - UITableViewDataSource
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fruits.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) as! TableViewCell
let fruitName = fruits[indexPath.row]
cell.productTitle.text = fruitName
cell.productImage.image = UIImage(named: fruitName)
cell.productPrice.text = PriceArray[indexPath.row]
cell.productsubTitle.text = categoryArray[indexPath.row]
return cell
}
}
This helped me.
result in picture below:
img
For inserting data into UITableViewcell use below code:
import UIKit
class ViewController: UIViewController,UITableViewDataSource {
#IBOutlet var tableView: UITableView!
var dataArray:NSArray!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.dataSource = self
dataArray = NSArray(objects: "a","b","c")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = dataArray.object(at: indexPath.row) as? String
return cell
}
}
tableView is outlet of UItableView.
You can populate an UITableView from an array like below:
(assuming that your array has a list of string values):
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Creating the tableView cell
let tableViewCell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! UITableViewCell
//Assigning values
tableViewCell.lblName?.text = array.object(at: indexPath.row) as? String
return tableViewCell
}
In this way you can show the value from your array to the label in your tableView.