2017-08-21 7 views
0

私はviewcontrollerクラスにプロパティをフックアップしましたが、セクション内ですべてのセルを返すことができません。私は各セルの間に10pixのマージンを必要とし、別のVCでこれを成功させることができましたが、私が使用している方法は1つのセルを返すだけです(合計2セルしかありません)。セクション内のすべてのセルを表示し、コードが以下に含まれている:セクションビュー内のすべてのセルが返されていません

//UITableViewDataSource 

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let headerView = UIView() 

    headerView.layer.cornerRadius = 8.0 
    headerView.layer.masksToBounds = true 
    headerView.backgroundColor = UIColor.colorWithHex("A171FF") 

    let headerLabel = UILabel(frame: CGRect(x: 30, y: 0, width: 
     tableView.bounds.size.width, height: tableView.bounds.size.height)) 
    headerLabel.font = UIFont(name: "Gill Sans", size: 15) 
    headerLabel.textColor = UIColor.white 
    headerLabel.text = self.tableView(self.myWldTbl, titleForHeaderInSection: section) 
    headerLabel.sizeToFit() 
    headerView.addSubview(headerLabel) 

    return headerView 
} 

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 

    return sections[section] 
} 

func numberOfSections(in tableView: UITableView) -> Int { 
    return sections.count 
} 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    switch (section) { 
    case 0 : 
    return userMoves.count 
    case 1: 
    return rsvps.count 
    default : 
    print("unable to set up sections") 
    return 0 
    } 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if indexPath.row % 2 != 0 { 
     let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: MyWorldTableViewCell.self)) as! MyWorldTableViewCell 

     //cell appearance 
     cell.layer.cornerRadius = 8.0 
     cell.clipsToBounds = true 

     //cell data 
    let evt = userMoves[indexPath.row] 
    cell.rsvpCount.text = "\(rsvps.count)" 

    //evt img 
    if let evtImg = evt.event_photo_url { 
     cell.img.kf.setImage(with: URL(string: Constants.Server.PHOTO_URL + evtImg)) 
    } else { 
     cell.img.image = UIImage(named: "user_icon") 
    } 
    cell.ttl.text = evt.event_name! 

    return cell } else { 
     let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: InvisibleCell.self)) as! InvisibleCell 

     cell.backgroundColor = UIColor.clear 

     return cell 
    } 
} 


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    if indexPath.row % 2 == 0 { 
     return 10.0 
    } 

    return 102.0 
} 

//UITableViewDelegate 


func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 30 
} 
+0

私はこれを作ってみました計算が、カスタムセルを作成していたときに、インデックスがswitch文の範囲外であることを示すときにエラーが発生しました。それはuserMoves.count * 2で、目に見えない行も含めて表示します。 – NightHawk95

答えて

0

EDITED:

あなたのコードについて混乱されて最初の事はあなたが2つの異なる配列(の内容を示す2つのセクションを持っているように見えるということですロジックごとにnumberOfRowsInSection)、セクション番号はcellForRowAtでチェックしません。

他のものより前には、cellForRowAtindexPath.sectionをチェックして、正しいアレイを使用していることを確認する必要があります。書かれているように、コードではuserMoves配列を使用して両方のセクションにデータを格納しています。

欠けているセルの原因は、numberOfRowsInSectionメソッドの不可視セパレータセルを考慮する必要があることです。あなたは2で乗算する正しい考えを持っていた:

switch (section) { 
    case 0 : 
     return userMoves.count * 2 
    //etc 
} 

アレイにアクセスすると、cellForRowAtに、あなたが境界例外のうち、インデックスを避けるために2で割る必要があります。

if indexPath.row % 2 != 0 { 
    //dequeue, etc. 

    let evt = userMoves[indexPath.row/2] 

    //etc. 
} 
+0

アイデアは、行、私はセクションの条件をチェックするの背後に勢いが表示されません – NightHawk95

+0

私の答えは間違っていた。私はセクションの状態をチェックすることについて、あなたのコードの問題だったので、私が行っていたことを明確にしました。私は正解と思われるものを追加しました(サンプルプロジェクトでテスト済み)。 –

+1

この助けてくれてありがとう! – NightHawk95

関連する問題