2017-05-27 53 views
0

我在mainStoryboard上有兩個自定義單元格的tableView。TableView中的兩個自定義單元格佈局

我想在另一行設置兩個單元格。我試圖找到答案,但無法找到答案。

我在下面添加了圖像和代碼。

enter image description here

enter image description here

class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, MFMailComposeViewControllerDelegate { 

     @IBOutlet var tblStoryList: UITableView! 

     var array = PLIST.shared.mainArray 



     override func viewDidLoad() { 
      super.viewDidLoad() 


     //spacing between header and cell 
      self.tblStoryList.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0) 

      //delete separator of UITableView 
     tblStoryList.separatorStyle = .none 

     } 
     func numberOfSections(in tableView: UITableView) -> Int { 
      return 1 
     } 

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      return self.array.count + 1 
     } 



     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
      if indexPath.row == 0 { 
       let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell", for: indexPath) as! HeaderCell 
       cell.headerTitle.text = "First Stage" 
       return cell 
      } 

      let cell = tableView.dequeueReusableCell(withIdentifier: "StoryTableviewCell", for: indexPath) as! StoryTableviewCell 


      //making plist file 
      let dict = self.array[indexPath.row - 1] 
      let title = dict["title"] as! String 
      let imageName = dict["image"] as! String 
      let temp = dict["phrases"] as! [String:Any] 
      let arr = temp["array"] as! [[String:Any]] 
      let detail = "progress \(arr.count)/\(arr.count)" 

      //property to plist file 
      cell.imgIcon.image = UIImage.init(named: imageName) 
      cell.lblTitle.text = title 
      cell.lblSubtitle.text = detail 

      cell.selectionStyle = UITableViewCellSelectionStyle.none 


      return cell 
     } 

回答

1

更新您的條件HeaderCell並使用三元操作員設置headerTitle

if indexPath.row == 0 || indexPath.row == 3 || indexPath.row == 5 { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell", for: indexPath) as! HeaderCell 
    cell.headerTitle.text = indexPath.row == 0 ? "First Stage" : indexPath.row == 3 ? "Second Stage" : "Third Stage" 
    return cell 
} 
+0

它完美地工作。感謝您的幫助! – risa8

+0

當我實現代碼時,自定義單元會替換原始單元。你有任何想法來解決它? – risa8

+0

你能詳細說明嗎? –

0

代替使用階段作爲行的,使用它們作爲一個節頭。您可以將自定義視圖放在標題部分。

核心創意: 科IZE你的一行沿着階段,把單元格的數據在陣列中,並與部分價值作爲其主要的字典添加。爲了顯示你的情況,字典將如下所示。

@{ 
    @"First Stage" : @[object_for_basic_grammar_1, 
         object_for_basic_grammar_2 
         ], 
    @"Second Stage": @[object_for_basic_grammar3], 
    ... 
    ... 
} 

而且需要另一個陣列存儲字典鍵的順序,將可視化到以下

@[@"First Stage", @"Second Stage" 
] 

現在一步一步按照列表:

  1. 使用numberOfSections(in:)爲規定這裏有多少個階段。返回存儲節的數組的數量。
  2. 使用tableView(_:numberOfRowsInSection:)提供該部分中的行數。從稍後聲明的節列表數組中獲取節串對象並搜索字典中的行。您會在這裏找到一組語法,返回該數組的計數。
  3. 使用tableView(_:cellForRowAt:)爲您提供該行的特定單元格,就像您爲語法單元格完成的一樣。
  4. 使用tableView(_:heightForHeaderInSection:)提供節標題的高度。
  5. 現在執行tableView(_:viewForHeaderInSection:)並返回指定截面視圖的視圖,在您的情況下是階段。

希望它可以幫助你,祝你好運。

相關問題