2014-06-18 6 views
1

셀을 재사용하는 대신 미리 생성 된 셀을 사용하여 테이블 뷰 컨트롤러를 작성하고 있습니다. 현재 MyCustomCell 배열을 보유하고 있습니다. 예를 들면 다음과 같습니다.Swift UITableView 배열 배열에서 셀 반환

하자 섹션은 = [[CELL1, CELL2, CELL3가], [cell4, cell5가], [cell6]]는

따라서, 내 numberOfSectionsInTableView에서 나는 다음과 같은 일을 해요 :

override func numberOfSectionsInTableView(tableView: UITableView?) -> Int { 
     return sections.count 
} 

합니다. . 내 numberOfRowsForSection에서 내가하고있는 일 :

override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int { 
     return sections[section].count 
} 

이것은 모두 잘 작동하고 멋지다. 배열에서 셀을 잡으려고 할 때 나는 여러 가지 다른 문제를보고 더 힘든 시간을 보내고 있으며이를 구현하는 가장 좋은 방법을 알고 싶습니다. 다음은, 가장 간단한 방법 것 같다, 나는 몇 가지 다른 형태로 같은 일을 구현했지만, 난 결과 오류 프로젝트를 빌드하고 얻을 수 없다 얻을 :

override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? { 
     var arrayForSection = sections[indexPath!.section] 
     var cell: MyCustomCell = arrayForSection[indexPath!.row] as MyCustomCell 
     return cell 
} 
  1. While emitting SIL for 'tableView' at /Users/blah/blah/blah/MyCustomCell/MyCustomCell Example/MyCustomCell Example/ExampleTableViewController.swift:125:14 :0: error: unable to execute command: Segmentation fault: 11 :0: error: swift frontend command failed due to signal (use -v to see invocation) Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254

을 여기에 어떤 문제가 있는지 잘 모르겠지만 간단한 구현을 통해 적절한 셀을 반환하고 실제로 컴파일 할 수 있습니까? 에 대한 자세한 특정되고, 또한

override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? { 
     let cellsForSection: MyCustomCell[] = sections[indexPath!.section] as Array 
     return cellsForSection[indexPath!.row] as MyCustomCell 
} 

: 내가이 독방 감금 - 장애로보고 된 것은 버그 아니지만, 다음에 cellForRowAtIndexPath의 코드를 변경하는 트릭을 할 것 인 경우

답변

0

확실하지 않음 변수를 만들 때 유형이 더 간단합니다. 이 변경이에

var sections = [] 
    var firstSectionCells = [] 
    var secondSectionCells = [] 
    var thirdSectionCells = [] 
    sections = [firstSectionCells, secondSectionCells, thirdSectionCells] // I do this later 

:

var sections: MyCustomCell[][] = [] 
    var firstSectionCells: MyCustomCell[] = [] 
    var secondSectionCells: MyCustomCell[] = [] 
    var thirdSectionCells: MyCustomCell[] = [] 
    sections = [firstSectionCells, secondSectionCells, thirdSectionCells] // I do this later 
인 대신에이 작업을 수행 할 수있는에

결과보다 간결 조금 :

override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? { 
     let cellsForSection = sections[indexPath!.section] 
     return cellsForSection[indexPath!.row] 
    }