具有多个单元格的TableView

时间:2019-05-15 22:54:55

标签: swift

我想打两个电话。只要chipnumber2不为空,我想调用cellnew。但是“ let deviceItem:Device3New = itemsNew [indexPath.row]”行遇到问题。此错误:致命错误:索引超出范围。如何调用两个单元函数?调用Cellnew时会发生此错误,但是调用cell时没有问题。那么如何在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

        }

    }

1 个答案:

答案 0 :(得分:1)

创建具有通用属性的协议设备

protocol Device {
    //common properties of both struct
    var time: String { get }
    var title: String { get }
    var places: String { get }
    var state: String { get }
}

在两个结构中均确认此协议,并在需要时添加不常见的属性

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
}

使用上述协议的类型创建一个公共数组

现在在numberOfRowsInSection方法中检查chipnumber2.text!.isEmptychipnumber.text!.isEmpty并将数组追加到公共数组。然后在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
    }
}