2017-12-20 12 views
0

두 가지 유형의 셀이있는 tableview가 있습니다. 이미지가있는 이미지와 기타 이미지는 텍스트 만있는 평범한 이미지입니다. 사용자가 행 중 하나를 탭하면 tableview가 드릴 다운되고 새 tableview가 열립니다 (iOS의 설정처럼). 나는 데이터 모델 아래에 있었지만 한 페이지에서만 작동합니다. MVVC로 중첩 된 것들을 어떻게 구현할 수 있습니까? MVVC 튜토리얼을 많이 검색했지만 내 상황을 파악할 수 없습니다. 예를 들어 "Documents"라는 한 행을 의미하고 문서를 탭하면 "First Doc", "Second Doc"등과 같은 모든 문서를 나열하려고합니다.이 절차는 모든 행에 대해 동일합니다.드릴 다운을위한 MVVC 구현 두 종류의 셀을 사용하는 UITableview

Model Picture

protocol SameCell{ 
    var type: CellType { get } 
} 
struct LeftMenuSimpleCellData: SameCell{ 
    let cellText: String 
    let haveIndicator: Bool 
    let sub: subTable? 
    let type: CellType 
} 
struct LeftMenuCarCellData: SameCell{ 
    let carImage: String 
    let plateText: String 
    let modelText: String 
    let haveIndicator: Bool 
    let sub: subTable? 
    let type: CellType 

} 
struct subTable{ 
    let cellText: String 
    let haveIndicator: Bool 
} 
enum CellType{ 
    case LeftMenuSimpleCellData 
    case LeftMenuCarCellData 
} 
class LeftMenuDataModel{ 

    var optionsData: [SameCell] = [] 

    let carData: [LeftMenuCarCellData] = [] 

    init(){ 

    } 

func populateOptionsData(){ 
    optionsData.append(LeftMenuCarCellData(carImage: "icon", plateText: "Car", modelText: "Model Name", haveIndicator: true,sub: nil, type: .LeftMenuCarCellData)) 
    optionsData.append(LeftMenuSimpleCellData(cellText: "Hello", haveIndicator: false, sub: nil,type: .LeftMenuSimpleCellData)) 
    optionsData.append(LeftMenuSimpleCellData(cellText: "Hello", haveIndicator: true, sub: nil,type: .LeftMenuSimpleCellData)) 
    optionsData.append(LeftMenuSimpleCellData(cellText: "Hello", haveIndicator: true, sub: nil,type: .LeftMenuSimpleCellData)) 
    optionsData.append(LeftMenuSimpleCellData(cellText: "HEllo", haveIndicator: true, sub: nil,type: .LeftMenuSimpleCellData)) 
    optionsData.append(LeftMenuSimpleCellData(cellText: "Hello", haveIndicator: true, sub: nil,type: .LeftMenuSimpleCellData)) 
    optionsData.append(LeftMenuSimpleCellData(cellText: "Hello", haveIndicator: true, sub: nil,type: .LeftMenuSimpleCellData)) 
    optionsData.append(LeftMenuSimpleCellData(cellText: "Hello", haveIndicator: false, sub: nil,type: .LeftMenuSimpleCellData)) 
    optionsData.append(LeftMenuSimpleCellData(cellText: "Exit", haveIndicator: false, sub: nil,type: .LeftMenuSimpleCellData)) 
} 

편집 : 나는 검색으로 "키 : 값"배열 일 수 있습니다. 그러나 아래 코드는 "LeftMenuSimpleCellData"가 프로토콜 'Hashable'오류를 준수하지 않는다고 설명합니다.

var optionsData: [LeftMenuSimpleCellData: [LeftMenuSimpleCellData] ]? 

편집 2 : 구조체를 재귀 적으로 변경할 수 있습니까?

struct LeftMenuSimpleCellData{ 
let cellText: String 
let haveIndicator: Bool 
let subMenu: LeftMenuSimpleCellData 
} 

편집 3 : 내 코드를 편집했습니다. 그것은 특히 지금 일하고 있습니다. 하지만 아직 두 번째 하위 메뉴를 구현하지 않았습니다.

답변

0

귀하의 질문과 의견에 따르면, 여기에 더미 ViewModel 구조를 만든 마지막 수정 된 답변입니다. 이 참조를 가져 와서 자신의 ViewModel을 만들어보십시오. 그래서 여기 우리가 간다 .... RootNode ViewModel을 받고 지금

class LeftMenuSimpleCellData{ 
    let cellText: String 
    let haveIndicator: Bool 
    var nextVM : ViewModel? 
    init(_ text : String , indicator : Bool = true) { 
     self.cellText = text 
     self.haveIndicator = indicator 

    } 
} 
class ViewModel : NSObject 
{ 


    private override init() { 
    } 
    init(data : [LeftMenuSimpleCellData]) 
    { 

     self.cellData = data 
    } 
    private var cellData = [LeftMenuSimpleCellData]() 

    var count : Int {return cellData.count} 



    func cellData(at index : Int) -> LeftMenuSimpleCellData { 
     return cellData[index] 
    } 

} 

, 나는detail : [[String : Any]]

[ 
    { 
    "name" : "Wi Fi", 
    "Sub Menu" : [ 
     { 
     "name" : "Wi Fi1", 
     "Sub Menu" : [ 

     ] 
     }, 
     { 
     "name" : "Wi Fi2", 
     "Sub Menu" : [ 

     ] 
     } 
    ] 
    }, 
    { 
    "name" : "Blutooth", 
    "Sub Menu" : [ 
     { 
     "name" : "Blutooth1", 
     "Sub Menu" : [ 

     ] 
     }, 
     { 
     "name" : "Blutooth2", 
     "Sub Menu" : [ 

     ] 
     } 
    ] 
    }, 
    { 
    "name" : "General", 
    "Sub Menu" : [ 
     { 
     "name" : "About", 
     "Sub Menu" : [ 
      { 
      "name" : "Name", 
      "Sub Menu" : [ 

      ] 
      }, 
      { 
      "name" : "Network", 
      "Sub Menu" : [ 

      ] 
      } 
     ] 
     }, 
     { 
     "name" : "Accessibility", 
     "Sub Menu" : [ 
      { 
      "name" : "VoiceOver", 
      "Sub Menu" : [ 

      ] 
      }, 
      { 
      "name" : "Speech", 
      "Sub Menu" : [ 
       { 
       "name" : "Speech Selection", 
       "Sub Menu" : [ 

       ] 
       } 
      ] 
      } 
     ] 
     } 
    ] 
    } 
] 

이이 기능 여기

func getVM(for detail : [[String : Any]]) -> ViewModel { 
     let leftCellMenu = detail.map {[weak self] (det) -> LeftMenuSimpleCellData in 

      let leftCellData = LeftMenuSimpleCellData.init(det["name"] as! String) 
      if let arr = det["Sub Menu"] as? [[String : Any]] , !arr.isEmpty { 
       leftCellData.nextVM = self?.getVM(for: arr) 
      } 
      else 
      { 
       leftCellData.nextVM = nil 
      } 
      return leftCellData 
     } 

     return ViewModel(data: leftCellMenu) 
    } 

인에게 더미 데이터를 작성했습니다 mainViewController에 대한 rootVM을 제공합니다.

이제 mainViewController tableView 데이터 소스 방법

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

     let subMenu = vm.cellData(at: indexPath.row) 
     cell.textLabel?.text = subMenu.cellText 
     cell.accessoryType = subMenu.nextVM == nil ? .none : .disclosureIndicator 
     return cell 
    } 

그리고 나는 이것이 당신이 원하는 것을 달성하는 데 도움이됩니다 이제 희망 tableView 대리자 메서드

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let subMenu = vm.cellData(at: indexPath.row) 
     guard subMenu.nextVM != nil else { 
      return 
     } 
     let mainVC = self.storyboard?.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController 
     mainVC.vm = subMenu.nextVM! 
     navigationController?.pushViewController(mainVC, animated: true) 
    } 

합니다. 검색어가 있으면 의견을 보내주십시오.

+0

답변을 이해하지만 무엇이 2 개 하위 메뉴가 있습니까? 나는 세부 사항이 추가 될 때마다 새 하위 메뉴가 생길 때마다 [Settings : [Detail]]처럼 동적으로 디자인 할 수 있습니까? –

+0

구조체 A와 같은 재귀 적 모델 {let subMenu : A} –

+0

모델 응답을 보여 줄 수 있습니까? 그 생각을하는 데 도움이 될 것입니다. –