使用自定义TableView索引超出范围错误

时间:2017-02-05 19:26:48

标签: ios iphone swift uitableview swift3

我有两个TableView,两个都是Custom。其中之一是带有Header单元格的Custom TableView。第二个是没有标题单元格的自定义TableView。我想要做的是当有人点击第一个tableView时,被点击的项目被添加到第二个TableView。问题是当我尝试添加第二个项目时,应用程序崩溃时出现索引超出范围的致命错误。这是在Swift 3中。

这是我的代码:

    import UIKit

struct TableData {

    var section: String = ""

    var data = Array<String>()

    var dataS = Array<String>()

    init(){}

}

var data = Array<TableData>()
var wordData = Array<TableData>()

class MyCustomCell: UITableViewCell {

    @IBOutlet var label: UILabel!

    @IBOutlet var labelS: UILabel!

}

class MyCustomWordCell: UITableViewCell {

    @IBOutlet var wordLabel: UILabel!
    @IBOutlet var wordLabelS: UILabel!

}

class MyCustomHeader: UITableViewCell {

    @IBOutlet var header: UILabel!

}

class TypeViewController: BaseViewController , UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableView: UITableView!

    @IBOutlet var wordTableView: UITableView!

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if tableView == self.tableView {
            return data[section].data.count
        }

        if tableView == self.wordTableView {
            return wordData.count
        }

        return 0
    }


    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if tableView == self.tableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TypeCell", for: indexPath) as! MyCustomCell
            cell.label.text = data[indexPath.section].data[indexPath.row]
            cell.labelS.text = data[indexPath.section].dataS[indexPath.row]
            return cell

        }

        if tableView == self.wordTableView {

            let cell = tableView.dequeueReusableCell(withIdentifier: "WordCell", for: indexPath) as! MyCustomWordCell

            if wordData.count != 0 {
                if wordData.count > indexPath.row {
                    cell.wordLabel.text = wordData[indexPath.row].data[0]
                    cell.wordLabelS.text = wordData[indexPath.row].dataS[0]
                }
            }
            return cell
        }
        return UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")

    }

    func numberOfSections(in tableView: UITableView) -> Int {

        if tableView == self.tableView {

            return 7

        } else {

            return 1

        }

    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        if tableView == self.tableView {

            let  headerCell = tableView.dequeueReusableCell(withIdentifier: "Header") as! MyCustomHeader

            headerCell.header.text = data[section].section

            return headerCell

        }

        return UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Header")

    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50.0
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        if tableView == self.tableView {

            var element: TableData

            element = TableData()

            element.data.append(data[indexPath.section].data[indexPath.row]);

            element.dataS.append(data[indexPath.section].dataS[indexPath.row]);

            wordData.append(element)

            wordTableView.reloadData()

            if wordData.count == 3 {

                performSegue(withIdentifier: "segueFind", sender: self)

            }

        }

        if tableView == self.wordTableView {

            wordData.remove(at: indexPath.row)

            wordTableView.reloadData()

        }

    }

    override func viewDidLoad() {
        super.viewDidLoad()
        addSlideMenuButton()
        addItems()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func addItems() {

        var new_elements:TableData

        new_elements = TableData()
        new_elements.section = "Name"
        new_elements.data.append(obj1);
        new_elements.data.append(obj2);
        new_elements.data.append(obj3);
        new_elements.data.append(obj4);
        new_elements.data.append(obj5);
        new_elements.data.append(obj6);
        new_elements.data.append(obj7);
        new_elements.dataS.append(objS1);
        new_elements.dataS.append(objS2);
        new_elements.dataS.append(objS3);
        new_elements.dataS.append(objS4);
        new_elements.dataS.append(objS5);
        new_elements.dataS.append(objS6);
        new_elements.dataS.append(objS7);

        data.append(new_elements)

        new_elements = TableData()
        new_elements.section = "Name"
        new_elements.data.append(obj11);
        new_elements.data.append(obj12);
        new_elements.data.append(obj13);
        new_elements.data.append(obj14);
        new_elements.data.append(obj15);
        new_elements.data.append(obj16);
        new_elements.data.append(obj17);
        new_elements.dataS.append(objS11);
        new_elements.dataS.append(objS12);
        new_elements.dataS.append(objS13);
        new_elements.dataS.append(objS14);
        new_elements.dataS.append(objS15);
        new_elements.dataS.append(objS16);
        new_elements.dataS.append(objS17);

        data.append(new_elements)

        new_elements = TableData()
        new_elements.section = "Name"
        new_elements.data.append(obj21);
        new_elements.data.append(obj22);
        new_elements.data.append(obj23);
        new_elements.data.append(obj24);
        new_elements.data.append(obj25);
        new_elements.data.append(obj26);
        new_elements.data.append(obj27);
        new_elements.dataS.append(objS21);
        new_elements.dataS.append(objS22);
        new_elements.dataS.append(objS23);
        new_elements.dataS.append(objS24);
        new_elements.dataS.append(objS25);
        new_elements.dataS.append(objS26);
        new_elements.dataS.append(objS27);

        data.append(new_elements)

        new_elements = TableData()
        new_elements.section = "Name"
        new_elements.data.append(obj31);
        new_elements.data.append(obj32);
        new_elements.data.append(obj33);
        new_elements.data.append(obj34);
        new_elements.data.append(obj35);
        new_elements.data.append(obj36);
        new_elements.data.append(obj37);
        new_elements.dataS.append(objS31);
        new_elements.dataS.append(objS32);
        new_elements.dataS.append(objS33);
        new_elements.dataS.append(objS34);
        new_elements.dataS.append(objS35);
        new_elements.dataS.append(objS36);
        new_elements.dataS.append(objS37);

        data.append(new_elements)

        new_elements = TableData()
        new_elements.section = "Name"
        new_elements.data.append(obj41);
        new_elements.data.append(obj42);
        new_elements.data.append(obj43);
        new_elements.data.append(obj44);
        new_elements.data.append(obj45);
        new_elements.data.append(obj46);
        new_elements.data.append(obj47);
        new_elements.dataS.append(objS41);
        new_elements.dataS.append(objS42);
        new_elements.dataS.append(objS43);
        new_elements.dataS.append(objS44);
        new_elements.dataS.append(objS45);
        new_elements.dataS.append(objS46);
        new_elements.dataS.append(objS47);

        data.append(new_elements)

        new_elements = TableData()
        new_elements.section = "Name"
        new_elements.data.append(obj51);
        new_elements.data.append(obj52);
        new_elements.data.append(obj53);
        new_elements.data.append(obj54);
        new_elements.data.append(obj55);
        new_elements.data.append(obj56);
        new_elements.data.append(obj57);
        new_elements.dataS.append(objS51);
        new_elements.dataS.append(objS52);
        new_elements.dataS.append(objS53);
        new_elements.dataS.append(objS54);
        new_elements.dataS.append(objS55);
        new_elements.dataS.append(objS56);
        new_elements.dataS.append(objS57);

        data.append(new_elements)

        new_elements = TableData()
        new_elements.section = "Name"
        new_elements.data.append(obj61);
        new_elements.data.append(obj62);
        new_elements.data.append(obj63);
        new_elements.data.append(obj64);
        new_elements.data.append(obj65);
        new_elements.data.append(obj66);
        new_elements.data.append(obj67);
        new_elements.dataS.append(objS61);
        new_elements.dataS.append(objS62);
        new_elements.dataS.append(objS63);
        new_elements.dataS.append(objS64);
        new_elements.dataS.append(objS65);
        new_elements.dataS.append(objS66);
        new_elements.dataS.append(objS67);

        data.append(new_elements)


    }

}

给出错误的行如下:

if wordData.count > indexPath.row {

                 cell.wordLabel.text = wordData[indexPath.section].data[indexPath.row] //This one


                    cell.wordLabelS.text = wordDataS[indexPath.section].dataS[indexPath.row]

                }

附上了MainStoryboard的一些照片:

https://www.dropbox.com/sh/s5k50ubas8wewo8/AACTkAsPDtgbOk3EOrb0LeDJa?dl=0

1 个答案:

答案 0 :(得分:0)

您的数据结构在这里并没有真正帮助您,但您的问题是在indexPath.section cellForRow:at:函数中使用wordTableView

此功能应使用wordData[indexPath.row].data[0]

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if tableView == self.tableView { 
        let cell = tableView.dequeueReusableCell(withIdentifier: "TypeCell", for: indexPath) as! MyCustomCell    
        cell.label.text = data[indexPath.section].data[indexPath.row]
        cell.labelS.text = dataS[indexPath.section].dataS[indexPath.row]   
        return cell

    }

    if tableView == self.wordTableView {

        let cell = tableView.dequeueReusableCell(withIdentifier: "WordCell", for: indexPath) as! MyCustomWordCell

        if wordData.count != 0 {
            if wordData.count > indexPath.row {
                cell.wordLabel.text = wordData[indexPath.row].data[0]            
                cell.wordLabelS.text = wordDataS[indexPath.row].dataS[0]      
            }        
        }
        return cell          
    }
    return UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")    
}

此外,您的numberOfRowsInSection不正确。它应该是:

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if tableView == self.tableView {   
        return data[section].data.count
    }

    if tableView == self.wordTableView {
        return wordData.count
    }

    return 0
}