根据单击的按钮在tableView中显示不同的数据

时间:2017-11-19 18:28:39

标签: ios swift uitableview

我在视图控制器的底部有一个tableview。我试图在tableview上显示不同的东西,具体取决于点击了哪个按钮。我有不同的原型单元格和配置方法用于数据设置,但是我无法得到最后一点。这是我的代码:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var count = Int ()
    if podsButton.isSelected {
        count = pods.count
    } else if subscribedButton.isSelected {
        count = subscribers.count
    } else if subscribersButton.isSelected {
        count = subscribed.count
    }
    return count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell ()
    if podsButton.isSelected{
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell") as? ProfileTableViewCell else {
            return UITableViewCell() }
        let pod = pods[indexPath.row]
        cell.configureCell(pod: pod)
    } else if subscribedButton.isSelected {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "FollowingCell") as? FollowingTableViewCell else {
            return UITableViewCell() }
        let user = subscribed[indexPath.row]
        cell.configureCell(user: user)
    }
    return cell
}

现在桌面视图没有显示任何内容。我在cellForRow中返回的单元格只是变量,而不是一个正确出列的条件选项吗?这是我的问题。第一个选项(ProfileCell)也应该是默认选项。谢谢你的帮助。

更新:

现在使用我的枚举我的代码如下:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var count = 0
    if case .pods = displayState {
        count = pods.count
    } else if case .subscribed = displayState{
        count = subscribers.count
    } else if case .subscribers = displayState {
        count = subscribed.count
    }
    return count
}

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

    if case .pods = displayState{
        let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell") as! ProfileTableViewCell
        let pod = pods[indexPath.row]
        cell.configureCell(pod: pod)
        return cell
    } else if case .subscribed = displayState {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FollowingCell") as! FollowingTableViewCell
        let user = subscribed[indexPath.row]
        cell.configureCell(user: user)
        return cell
    } else if case .subscribers = displayState {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FollowingCell") as! FollowingTableViewCell
        let user = subscribers[indexPath.row]
        cell.configureCell(user: user)
        return cell
    } else {
        return UITableViewCell()
    }
}

我在按钮操作中设置displayStates,如下所示:

@IBAction func podsButtonClicked(_ sender: BottomBorderButton) {
    displayState = .pods
    podsButton.addBottomBorderWithColor(color: greenColor(), width: 3)
    subscribedButton.removeBottomBorder()
    subscribersButton.removeBottomBorder()
    self.tableview.reloadData()
}

我认为这是有效的,但我现在有一个Parse查询问题:

    // Query for the users that the user is subscribed to
    let subscribedQuery = Following.query()
    subscribedQuery?.whereKey("follower", equalTo: PFUser.current()?.objectId as Any)
    subscribedQuery?.findObjectsInBackground(block: { (objects, error) in
        if let objects = objects {
            for object in objects {
                self.subscribed.insert(object as! PFUser, at: 0)
            }
        }
    })

错误:

  

无法将'Speakable.Following'(0x100552ec0)类型的值转换为'PFUser'(0x100a12808)。

UPDATE ***

使用includeKey解决此问题,但现在正在

***由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'此查询具有出色的网络连接。你必须等到它完成。'

1 个答案:

答案 0 :(得分:0)

您应该在代码中进行更改。

确保在menu = fields.Many2many(comodel_name='lunch.order.line', default=lambda self:[self.favorite_menus()]) 中选择一些初始按钮。确保在按钮操作中拨打viewDidLoad

你应该正确地重构你的reloadData。在您当前的代码中,cellForRowAt的{​​{1}}变量与第一个cell之前的guard变量不同,因此您实际上始终返回空的cell实例你不必要地创造。

if

这是对力量铸造的正确使用。不需要UITableViewCell语句。如果您的单元格设置不正确,您希望此代码在开发期间崩溃。一旦设置正确,它在运行时就不会出错。

此外,func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if podsButton.isSelected{ let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell") as! ProfileTableViewCell let pod = pods[indexPath.row] cell.configureCell(pod: pod) return cell } else if subscribedButton.isSelected { let cell = tableView.dequeueReusableCell(withIdentifier: "FollowingCell") as! FollowingTableViewCell let user = subscribed[indexPath.row] cell.configureCell(user: user) return cell } else if subscribersButton.isSelected { // similar code to create and return cell } else { fatalError("Unexpected - no button selected") // Fix your code return UITableViewCell() } } 的第一行应为:

guard
相关问题