自定义TableView和标题

时间:2017-01-31 04:15:44

标签: ios swift scroll tableview

我有一个带有Header单元格的Custom TableView。问题是我只能显示一组数据而TableView当前不会滚动(可能是因为只显示了一组数据。)这是在Swift 3中。

这是我的代码:

的ViewController:



  

  

struct TableData {

        var section: String = ""
        
        var data = Array<String>()
        
        var dataS = Array<String>()
        
        init(){}

    }

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

    class MyCustomCell: UITableViewCell {

        @IBOutlet var label: UILabel!
        
        @IBOutlet var labelS: UILabel!
        
    }

    class MyCustomHeader: UITableViewCell {

        @IBOutlet var header: UILabel!
        
    }

    class TypeViewController: BaseViewController , UITableViewDelegate, UITableViewDataSource {
        
        @IBOutlet var tableView: UITableView!
        
        @IBOutlet var scrollView: UIScrollView!

        public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
            return data[section].data.count
        
        }
        
        public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MyCustomCell
            
            cell.label.text = data[indexPath.section].data[indexPath.row]
            cell.labelS.text = dataS[indexPath.section].data[indexPath.row]
            
            return cell

           
        }
        
        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            
            let  headerCell = tableView.dequeueReusableCell(withIdentifier: "Header") as! MyCustomHeader
            
            headerCell.header.text = data[section].section
            
            return headerCell

        }
        
        func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return 50.0
        }
        
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        print(indexPath.row)
        
        var element: TableData
        
        element = TableData()
        
        element.data.append(data[indexPath.section].data[indexPath.row]);
        
        wordData.append(element)
        
        element = TableData()
        
        element.data.append(dataS[indexPath.section].dataS[indexPath.row]);
        
        wordDataS.append(element)
        
        if wordData[indexPath.section].data.count == 3 {
            
            performSegue(withIdentifier: "segueFind", sender: self)
            
        }
        
    }
        
        override func viewDidLoad() {
            super.viewDidLoad()
            addSlideMenuButton()
            addItems()
            
            print(data)
            
        }

        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 = "Gevurah - Judgement"
        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);
        
        data.append(new_elements)
        
        new_elements = TableData()
        new_elements.section = "Hod - Reverberation"
        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);
        
        data.append(new_elements)
        
        new_elements = TableData()
        new_elements.section = "Tiferet - Beauty"
        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);
        
        data.append(new_elements)

 new_elements = TableData()
        new_elements.section = "Gevurah - Judgement"
        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);
        
        dataS.append(new_elements)
        
        new_elements = TableData()
        new_elements.section = "Hod - Reverberation"
        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);
        
        dataS.append(new_elements)
        
        new_elements = TableData()
        new_elements.section = "Tiferet - Beauty"
        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);
      
        dataS.append(new_elements)

}
&#13;
&#13;
&#13;

此外,我在此行上收到索引超出范围错误:

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

附上了MainStoryboard的一些照片:

https://i.stack.imgur.com/Yvw7I.png

i.stack.imgur.com/nLWZJ.png

https://i.stack.imgur.com/ralOb.png

1 个答案:

答案 0 :(得分:0)

在课堂上放置一些部分代码,肯定会有效。

public func numberOfSections(in tableView: UITableView) -> Int
{
    return dummyArray.count
}