2016-09-21 10 views
0

私はスウィフトに新しいですし、私はテーブルビューを作成しようとしています、私は唯一の3行(午前、午後、夜を)持っています。朝の行をクリックすると、セルが展開されます。それはkeynumber時間8 - 12のユーザー数を表示する必要があります。午後と夜に同じです。内のUITableView細胞

配列データはUser[]で、keynumber、時刻などのデータがあります。

私はifの条件をcellForRowAtIndexPathにしようとしましたが、モーニング行には1つのユーザーキーしか表示されません。その特定の期限内に別のユーザーキーを表示しません。

答えて

0

試しているが、ここでは魔法のように動作

struct User { 
    var keynumber: Int 
    var time: NSData 
} 

class TableViewController: UITableViewController { 

var userArr : Array<User> = [] //raw user data 

var sortedUser : [String : Array<User>] = ["Morning": [], "Afternoon": [], "Night": []] 
var sectionStatusArr : [String: Bool] = ["Morning": false, "Afternoon": false, "Night": false] // section status array used to control expanding 

override func viewDidLoad() { 
    super.viewDidLoad() 

    //classify user into corresponding group(time zone) 
    for _ in 0...19 { 
     let randomKey = Int(arc4random()%23) 
     let user = User(keynumber: randomKey, time: NSData()) 

     userArr.append(user) 
    } 

    //sort user: 8-12 -> morning, 13-18 -> afternoon, 19-8 -> night 
    for user in userArr{ 
     switch user.keynumber { 
     case 8,9,10,11,12: 
      sortedUser["Morning"]?.append(user) 
     case 13,14,15,16,17,18: 
      sortedUser["Afternoon"]?.append(user) 
     default: 
      sortedUser["Night"]?.append(user) 
     } 
    } 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Table view data source 

override func numberOfSections(in tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return sortedUser.keys.count 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 


    var rowcount = 0 
    var shouldExpandSection = false 
    switch section { 
    case 0: 
     rowcount = (sortedUser["Morning"]?.count)! 
     shouldExpandSection = sectionStatusArr["Morning"]! 
    case 1: 
     rowcount = (sortedUser["Afternoon"]?.count)! 
     shouldExpandSection = sectionStatusArr["Afternoon"]! 
    default: 
     rowcount = (sortedUser["Night"]?.count)! 
     shouldExpandSection = sectionStatusArr["Night"]! 
    } 
    return shouldExpandSection ? rowcount : 0 
} 

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return 40 
} 

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 60 
} 

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let header = UIView(frame: CGRect(x: 0, y: 0, width: 375, height: 60)) 

    let nameBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 375, height: 60)) 
    nameBtn.backgroundColor = UIColor.cyan 
    nameBtn.tag = section 
    nameBtn.addTarget(self, action: #selector(self.headerTouched(sender:)), for: .touchUpInside) 

    switch section { 
    case 0: 
     nameBtn.setTitle("Morning", for: .normal) 
    case 1: 
     nameBtn.setTitle("Afternoon", for: .normal) 
    default: 
     nameBtn.setTitle("Night", for: .normal) 
    } 

    header.addSubview(nameBtn) 

    return header 
} 

func headerTouched(sender:UIButton){ 
    switch sender.tag { 
    case 0: 
     sectionStatusArr["Morning"] = !sectionStatusArr["Morning"]! 
    case 1: 
     sectionStatusArr["Afternoon"] = !sectionStatusArr["Afternoon"]! 
    default: 
     sectionStatusArr["Night"] = !sectionStatusArr["Night"]! 
    } 
    self.tableView.reloadData() 
} 


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

    var sectionArr : Array<User>= [] 
    switch indexPath.section { 
    case 0: 
     sectionArr = sortedUser["Morning"]! 
    case 1: 
     sectionArr = sortedUser["Afternoon"]! 
    default: 
     sectionArr = sortedUser["Night"]! 
    } 

    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) 

    for subview in cell.subviews{ 
     subview.removeFromSuperview() 
    } 

    let lab = UILabel(frame: CGRect(x: 0, y: 0, width: 375, height: 40)) 
    lab.text = "Un:" + String(sectionArr[indexPath.row].keynumber) 
    lab.textAlignment = .center 
    cell.addSubview(lab) 

    return cell 
} 

} 
+0

これはどのように動作しますか?user.keynumberを切り替えますか?また、#selector(self.headerTouched(sender :))のエラーが発生しています。 –

+0

これは単純なスイッチの使用方法です。あなたの分類ルールに従って、朝 - [8:00 - 12:00]、午後 - [12:00 - 18:00]、夜 - [18:00 - 8:00]。あなたのIDEは何ですか?このコードはXcode8とSwift3.0で動作します。明日、私は仕事に戻るとプロジェクト全体を添付します –

+0

[こちら](https://github.com/Millionaryearl/Stack-Overflow/tree/master/TableVC)から全体のプロジェクトをダウンロードできます –

1

この

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
    @IBOutlet var tableView: UITableView! 
    var items: [String] = ["We", "Heart", "Swift"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 
    } 


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.items.count; 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell 

     cell.textLabel?.text = self.items[indexPath.row] 

     return cell 
    } 

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 
     print("You selected cell #\(indexPath.row)!") 
    } 
} 

・ホープ、このことができますしてみてください。