2017-10-02 8 views
0

UITableViewの場合、2つの異なるセルのいずれかを使用しています。最初のものは内容を表示し、2番目のものは配列が空の場合に内容がないという簡単なラベルです。私はまた、どのアレイを使用するかを制御するセグメント化されたコントローラを切り替えています。私は両方の細胞のためのクラスを持っています。 2番目のラベルには、セグメント化されたコントローラに基づいて変更されるIBoutletがあります。私のコードは次のようになります:異なるセルを返すUITableView

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    var returnValue = 0 

    switch(mySegmentedControl.selectedSegmentIndex) { 
    case 0: 
     returnValue = accounts.count 
     break 
    case 1: 
     returnValue = user.count 
     break 
    case 2: 
     returnValue = mutual.count 
    default: 
     break 
    } 

    return returnValue 
} 

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

    let cell2 = tableView.dequeueReusableCell(withIdentifier: "noUsersCell") as! NoUsersTableViewCell 

    switch(mySegmentedControl.selectedSegmentIndex) { 
    case 0: 
     let user = self.accounts[indexPath.row] 

     cell.user = user 
     cell.selectionStyle = UITableViewCellSelectionStyle.none 
     cell.delegate = self 

     break 

    case 1: 
     let user = self.user[indexPath.row] 

     cell.user = user 
     cell.selectionStyle = UITableViewCellSelectionStyle.none 
     cell.delegate = self 

     break 

    case 2: 
     let user = self.mutual[indexPath.row] 

     cell.user = user 
     cell.selectionStyle = UITableViewCellSelectionStyle.none 
     cell.delegate = self 

     break 

    default: 
     break 
    } 

    return cell 
} 

これまでのところ、これは新しい機能であるため、「cell2」を追加しました。私はnumberOfRowsInSectionを使って配列が空であるかどうかをチェックし、空であれば1を返し、CellForRowAtの場合に条件を入れて配列が空であるかどうかを調べ、cell2を使う必要があると考えます。次に、cell2のテキストを変更することができます。しかし、私は休憩の後に単一のcell2を返す方法を知らない。何か案は?

+0

を行う必要があります - これはスウィフトあります。あなたの 'case'ステートメントに' break'ステートメントは必要ありません。 – rmaddy

答えて

1

私はこれを2つのセクションで行います。実データにはセクション0を使用し、「コンテンツなし」セルにはセクション1を使用します。

以下は、第1セクションに行がない場合、第2セクション(「内容なし」)のみを示しています。

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    var returnValue = 0 

    switch (mySegmentedControl.selectedSegmentIndex) { 
    case 0: 
     returnValue = accounts.count 
    case 1: 
     returnValue = user.count 
    case 2: 
     returnValue = mutual.count 
    default: 
     break 
    } 

    if section == 0 {  
     return returnValue 
    } else { 
     return returnValue == 0 ? 1 : 0 
    } 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if indexPath.section == 0 { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "ConnectCell", for: indexPath) as! ConnectTableViewCell 

     switch(mySegmentedControl.selectedSegmentIndex) { 
     case 0: 
      let user = self.accounts[indexPath.row] 
      cell.user = user 
      cell.selectionStyle = UITableViewCellSelectionStyle.none 
      cell.delegate = self 
     case 1: 
      let user = self.user[indexPath.row] 
      cell.user = user 
      cell.selectionStyle = UITableViewCellSelectionStyle.none 
      cell.delegate = self 
     case 2: 
      let user = self.mutual[indexPath.row] 
      cell.user = user 
      cell.selectionStyle = UITableViewCellSelectionStyle.none 
      cell.delegate = self 
     default: 
      break 
     } 

     return cell 
    } else { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "noUsersCell") as! NoUsersTableViewCell 

     switch(mySegmentedControl.selectedSegmentIndex) { 
     case 0: 
      cell.textLabel.text = "No accounts" 
     case 1: 
      cell.textLabel.text = "No users" 
     case 2: 
      cell.textLabel.text = "No mutual" 
     default: 
      break 
     } 

     return cell 
    } 
} 
+0

私はラベルのテキストを変更する2番目のセルを定義する "else"のセグメント化されたコントローラをオンに切り替えることができますか? – Sente

+0

絶対に。 「コンテンツなし」セルのテキストを設定するために必要なことは何でも行うことができます。私の更新を参照してください。 – rmaddy

+0

魅力的な作品! – Sente

0
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    let count = myArray.count 
    switch count { 
     case 0: 
      return 1 
     default: 
      return count 
    } 
} 


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let count = myArray.count 
    switch count { 
    case 0 : 
     let cell = ..... NoContentCell 
     return cell 
    default: 
     let cell = ... ContentCell // and in here do your other cells. 
     return cell 
    } 
} 

これはFYIトリック

関連する問題