ios - 根据点击的按钮在tableView中显示不同的数据

标签 ios swift uitableview

我在 View Controller 的底部有一个表格 View 。我试图根据单击的按钮在表格 View 上显示不同的内容。我有不同的原型(prototype)单元和数据设置的配置方法,但是我无法得到最后一点。这是我的代码:

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
}

现在表格 View 什么也不显示。我在 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()
    }
}

我在按钮操作中设置显示状态,如下所示:

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

我认为这是有效的,但我现在遇到了解析查询问题:

    // 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)
            }
        }
    })

错误:

Could not cast value of type 'Speakable.Following' (0x100552ec0) to 'PFUser' (0x100a12808).

更新***

使用 includeKey 修复了此问题,但现在正在获取

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

最佳答案

您的代码中有很多地方需要更改。

确保您在 viewDidLoad 中选择了某个初始按钮。确保在按钮操作中调用 reloadData

并且您应该正确重构您的 cellForRowAt 。在当前代码中,guardcell 变量与第一个 if 之前的 cell 变量不同,因此实际上,您总是返回不必要创建的空 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 语句。如果您的单元设置不正确,您希望此代码在开发过程中崩溃。一旦设置正确,运行时就不会出错。

此外,numberOfRowsInSection 的第一行应该是:

var count = 0

关于ios - 根据点击的按钮在tableView中显示不同的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47380437/

相关文章:

swift - Xcode WebApp - 在 iPhone/Safari 上启动时不居中

swift - 在 Swift 中仅颜色下划线

ios - 有没有办法检查推送通知设置是否被请求?

ios - UITableView 'hidden' 部分导致每次拉动刷新时分配更多内存

swift - TableView 在 IB 中设计时显示内容,但在我以编程方式编写时不显示内容,我做错了什么

ios - 无法从另一个 View Controller 在 WebView 中显示 URL

ios - UICollectionViewCell 中的 UIButton subview 未更改控件状态

ios - UISegmentedControl 内部 View 应在显示 View 后调用 API

ios - 为什么这个 View Controller 需要初始化器?

ios - `xcconfig` 文件中的条件用户定义变量