2017-07-27 79 views
1

我想要建立一个两个部分的TableView。第二部分应该填充数组中的数据。 Storyboard有一个静态单元格的TableView。有两个部分,每个部分都有一个单元格。Swift静态TableView添加额外的行

如果在textSectionTwo阵列,它只是正常工作中只有一个项目,但休息时也有它

*终止应用程序由于未捕获的异常“NSRangeException”,原因更多的项目:“* - [__ NSSingleObjectArrayI objectAtIndex:]:索引1超出范围[0 .. 0]”

代码如下:

enum TableViewSections { 
    case sectionOne 
    case sectionTwo 
} 

class TableViewController: UITableViewController { 

    struct Identifier { 
     static let sectionOneCell = "SectionOne" 
     static let sectionTwoCell = "SectionTwo" 
    } 

    let textSectionOne = "Section 1" 
    let textSectionTwo = ["Section 2 - Row 1", "Section 2 - Row 2"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.register(UITableViewCell.self, forCellReuseIdentifier: Identifier.sectionOneCell) 
     tableView.register(UITableViewCell.self, forCellReuseIdentifier: Identifier.sectionTwoCell) 

     tableView.reloadData() 
    } 

    // MARK: - Table view data source 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     switch sectionForIndex(section) { 

     case .sectionTwo?: 
      return textSectionTwo.count  // <- breaks when return is > 1 

     case nil: 
      fatalError("Unexpected value") 

     default: 
      return super.tableView(tableView, numberOfRowsInSection: section) 
     } 
    } 


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     switch sectionForIndex(indexPath.section) { 
     case .sectionOne?: 
      return self.tableView(tableView, sectionOneCellForRowAt: indexPath) 

     case .sectionTwo?: 
      return self.tableView(tableView, sectionTwoCellForRowAt: indexPath) 

     case nil: 
      fatalError("Unexpected value") 
     } 
    } 


    private func tableView(_ tableView: UITableView, sectionOneCellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: Identifier.sectionOneCell, for: indexPath) 
     cell.textLabel?.text = textSectionOne 
     return cell 
    } 


    private func tableView(_ tableView: UITableView, sectionTwoCellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: Identifier.sectionTwoCell, for: indexPath) 
     cell.textLabel?.text = textSectionTwo[indexPath.row] 
     return cell 
    } 


    func sectionForIndex(_ index: Int) -> TableViewSections? { 
     switch index { 
     case 0: 
      return .sectionOne 

     case 1: 
      return .sectionTwo 

     default: 
      return nil 
     } 
    } 

} 

编辑:
this sample code from Appple是类似于我的问题。他们使用静态单元格并通过代码添加内容。

+1

你认为* static *是什么意思? ;-)为什么注册这些单元格,因为它们是在Interface Builder中设计的呢? – vadian

+0

我知道静态的意思。我研究了这个[Apple的示例代码](https://developer.apple.com/library/content/samplecode/HomeKitCatalog/Introduction/Intro.html),并且有通过代码添加数据的静态单元。 – Lars

回答

2

静态表格视图用于呈现静态单元格UI和其中的数据的表格视图。它将仅使用您在Storyboard中制作的那些单元格。

解决方案:使用动态表格视图。它将允许您呈现尽可能多的行。