Reputation: 523
I would like to call two cell. as long as chipnumber2 is not empty, I'd like to call cellnew. But " let deviceItem: Device3New = itemsNew[indexPath.row]" line is experiencing problems.This error : fatal error: index out of range. How do I call two cell functions? This error occurs when calling Cellnew, but there is no problem when calling cell. So how do I call two cell functions in a Tableview?
class NewMainTableViewController: UITableViewController, UITextFieldDelegate, MiniTabBarDelegate, NVActivityIndicatorViewable {
var items: [Device3] = []
var itemsNew: [Device3New] = []
let cellId: String = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(DeviceTableViewCell2.self, forCellReuseIdentifier: cellId)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! DeviceTableViewCell2
if((chipnumber2.text?.isEmpty) != true) {
let cellNew = tableView.dequeueReusableCell(withIdentifier: cellIdNew, for: indexPath) as! DeviceTableViewCell2
let deviceItem: Device3New = itemsNew[indexPath.row]
cellNew.badgeColor = UIColor.flatLime
cellNew.badgeTextColor = .white;
cellNew.badgeFontSize = 13;
cellNew.badgeString = deviceItem.time
cellNew.badgeOffset = CGPoint(x:30.0, y:63)
cellNew.deviceItem3New = deviceItem
cellNew.titleNew.text = deviceItem.title
cellNew.title1New.text = deviceItem.places
cellNew.titlesaatNew.text = deviceItem.time
cellNew.buttonNew.isOn = deviceItem.state
cellNew.tablerow = String (indexPath.row)
cellNew.backgroundColor = UIColor(white: 1, alpha: 0.09)
return cellNew
}
if((chipnumber.text?.isEmpty) != true) {
let deviceItem: Device3 = items[indexPath.row]
cell.badgeColor = UIColor.flatLime
cell.badgeTextColor = .white;
cell.badgeFontSize = 13;
cell.badgeString = deviceItem.time
cell.badgeOffset = CGPoint(x:30.0, y:63)
cell.deviceItem3 = deviceItem
cell.title.text = deviceItem.title
cell.title1.text = deviceItem.places
cell.titlesaat.text = deviceItem.time
cell.button.isOn = deviceItem.state
cell.tablerow = String (indexPath.row)
cell.backgroundColor = UIColor(white: 1, alpha: 0.09)
return cell
}
return cell
}
}
Upvotes: 0
Views: 117
Reputation: 15748
Create a protocol Device with common properties
protocol Device {
//common properties of both struct
var time: String { get }
var title: String { get }
var places: String { get }
var state: String { get }
}
Confirm to this procol in both structs and add un common properties if you need
struct Device3New: Device {
var time: String
var title: String
var places: String
var state: String
//other properties
var myVar1: String
}
struct Device3: Device {
var time: String
var title: String
var places: String
var state: String
//other properties
var myVar2: String
}
Create a common array with the type of the above protocol
Now in numberOfRowsInSection
method check chipnumber2.text!.isEmpty
and chipnumber.text!.isEmpty
and append the arrays to the common array. And in cellForRowAt
get the object from the common array.
class NewMainTableViewController: UITableViewController {
var items: [Device3] = []
var itemsNew: [Device3New] = []
var joinedItems: [Device] = []
//chipnumber, chipnumber2 textfields
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
joinedItems.removeAll()
if !chipnumber2.text!.isEmpty {
joinedItems.append(contentsOf: itemsNew)
} else if !chipnumber.text!.isEmpty {
joinedItems.append(contentsOf: items)
}
return joinedItems.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellNew = tableView.dequeueReusableCell(withIdentifier: "cellIdNew", for: indexPath) as! DeviceTableViewCell2
let deviceItem: Device = joinedItems[indexPath.row]
//common properties
print(deviceItem.time)
print(deviceItem.title)
print(deviceItem.places)
print(deviceItem.state)
//other properties
if let deviceItem = joinedItems[indexPath.row] as? Device3New {
print(deviceItem.myVar1)
} else if let deviceItem = joinedItems[indexPath.row] as? Device3 {
print(deviceItem.myVar2)
}
return cellNew
}
}
Upvotes: 1