[英]TableView with multiple cell swift
I would like to call two cell. 我想打两个电话。 as long as chipnumber2 is not empty, I'd like to call cellnew.
只要chipnumber2不为空,我想调用cellnew。 But " let deviceItem: Device3New = itemsNew[indexPath.row]" line is experiencing problems.This error : fatal error: index out of range.
但是“ let deviceItem:Device3New = itemsNew [indexPath.row]”行遇到问题。此错误:致命错误:索引超出范围。 How do I call two cell functions?
如何调用两个单元函数? This error occurs when calling Cellnew, but there is no problem when calling cell.
调用Cellnew时会发生此错误,但是调用cell时没有问题。 So how do I call two cell functions in a Tableview?
那么,如何在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
}
}
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. 现在,在
numberOfRowsInSection
方法中,检查chipnumber2.text!.isEmpty
和chipnumber.text!.isEmpty
并将数组附加到公共数组。 And in cellForRowAt
get the object from the common array. 并在
cellForRowAt
从公共数组获取对象。
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.