2017-12-26 22 views
0

기존 목록에 섹션을 추가 할 수 있습니까? 아니면 어떻게 든 하드 코딩 할 수 있습니까? 그래서 3 행과 4 섹션으로 구분되며, 행 9, 10 섹션으로 구분되어 있으므로
에 내가 섹션을 추가하려고했으나 이들은 매우 성공하지 :Table View에 섹션을 기존 목록에 추가 하시겠습니까?

목록 파일 :

import Foundation 

class ListItem { 
    var section = "" 
    var listItem = "" 
    var description = "" 
    var extraInfo = "" 
    var counter: Int = 0 

    init(section: String, listItem: String, description: String, ekstraInfo: String) { 
     self.section = section 
     self.listItem = listItem 
     self. description = description 
     self.ekstraInfo = ekstraInfo 
    } 
}  

보기 컨트롤러 :

let staticList: [ListItem] = 
    [ 

     ListItem(section: "Section 1", listItem: "Bananas", description: "Yellow", ekstraInfo: "Bent"), 
     ListItem(section: "Section 2", listItem: "Apples", description: "Red", ekstraInfo: "Round"), 
     ListItem(section: "Section 3", listItem: "Strawberries", description: "Red", ekstraInfo: ""), 
     ListItem(section: "Section 4", listItem: "Carrots", description: "Orange", ekstraInfo: ""), 
     ListItem(section: "Section 5", listItem: "Lime", description: "Green", ekstraInfo: "Round"), 
    ]  

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? 
    { 
     if (tableView == MainTableView) 
     { 
      return staticList[section].section 
     }else 
     { 
      return nil 
     } 
    }  

편집 :

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
      let cell: UITableViewCell 
      cell = tableView.dequeueReusableCell(withIdentifier: "MenuCell", for: indexPath) 

      if let customCell = cell as? MenuCell 
      { 
       let itemIndex = indexPath.row 
       let listItem = staticList[itemIndex] 

       customCell.itemLabel.text = listItem.listItem 
       customCell.descriptionLabel.text = listItem.description 
       customCell.exstraInfoLabel.text = listItem.exstraInfo 
       customCell.counterLabel.text = "\(listItem.counter)" 

       customCell.delegate = self 


      } 
      return cell 

     } 
    } 
+0

staticList를'[[ListItem]]'배열로 배열하는 것이 쉬울 수 있습니다. 각 "section"은 ListItem의 배열입니다. –

답변

1

일부 하드 코딩 된 섹션과 예제를 공유합니다. 이것은 어떻게 작동하는지 이해하는 데 도움이됩니다.

let numberOfRows = [2, 3, 1, 4, 5] 

여기에는 행 수를 나타내는 정수 배열이 있습니다. 기본적으로 각 섹션 2와 5 절, 3 ... 5 개 행이 각각

추가 귀하의 UITableViewDataSource에 다음

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return numberOfRows[section] 
} 

이 당신에게 제공해야 UITableView (5 개) 섹션 데 2, 3, 1, 4, 각 섹션에 5 개의 행. numberOfRows 주위

재생 등 더욱 섹션 이상의 행을 얻을

EDIT : staticList 단일 차원 배열이기 때문에, 각 영역은 동일한 셀로드

이유이다. 따라서 각 섹션에서 각 섹션에 대해 indexPath.row이 0부터 시작하여 동일한 행이 계속 가져옵니다. 이 문제를 해결하려면 staticList을 2 차원 배열로 만드십시오.

let staticList: [[ListItem]] = [ 
    [ 
     ListItem(section: "Section 1", listItem: "Bananas", description: "Yellow", ekstraInfo: "Bent"), 
     ListItem(section: "Section 1", listItem: "Apples", description: "Red", ekstraInfo: "Round") 
    ], 
    [ 
     ListItem(section: "Section 2", listItem: "Strawberries", description: "Red", ekstraInfo: "") 
    ], 
    [ 
     ListItem(section: "Section 3", listItem: "Carrots", description: "Orange", ekstraInfo: ""), 
     ListItem(section: "Section 3", listItem: "Lime", description: "Green", ekstraInfo: "Round") 
    ] 
] 

지금 staticList 각 섹션에서 각각 2, 1, 2 ListItem의 3 개 섹션이 어떻게 ... 여기.

마지막으로, 당신은 일을 청소하기 위해 ListItem에서 section 속성을 제거 할 수 있습니다 BTW, ...

// let itemIndex = indexPath.row 
// let listItem = staticList[itemIndex] 

let listItem = staticList[indexPath.section][indexPath.row] 

을 cellForRowAtIndexPath 기능에 작은 변화를합니다. 그래도 아무것도 남기지 않아야합니다.

+0

답장을 보내 주셔서 감사합니다. 이걸 시도하면'numberOfRows'와'numberOfRowsInSection'이 잘 동작합니다. 그러나 목록은 모든 섹션에서 시작됩니다. 내가 여기서 무엇을 놓치고 있니? ListItem을 표준 일반 배열로 전환하려고했습니다. 그러나 모든 섹션의 시작 부분에서도 시작됩니다. – Eccles

+0

'cellForRowAtIndexPath'에 대한 코드를 보내 주시겠습니까? 나는 그 문제가 거기에 있다고 생각한다. – NSAdi

+0

질문을 수정했습니다 – Eccles