我想在单元格的顶部放置一个特定的值

问题描述 投票:0回答:2

我想从表视图顶部的服务器放置一个特定值,即我想在表视图顶部向用户发送反馈的第一行

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        tableView.tableFooterView = UIView(frame: .zero)
        if let cell = tableView.dequeueReusableCell(withIdentifier: "AllFeedbackCell", for: indexPath) as? AllFeedbackCell {
            cell.feedback = feedbacks?[indexPath.row]
            return cell
        }
        return UITableViewCell()
    }
 var feedback: Feedback? {
        didSet {
            if let username = feedback?.username, !username.isEmpty {
                userEmailLabel.text = username
            } else {
                if let userEmail = feedback?.email, let emailIndex = userEmail.range(of: "@")?.upperBound {
                    userEmailLabel.text = String(userEmail.prefix(upTo: emailIndex)) + "..."
                }
            }
            feedbackDateLabel.text = feedback?.timeStamp.getFirstChar(10)
            userFeedbackLabel.text = feedback?.feedbackString
            if let avatarURLString = feedback?.avatar {
                let imageURL = URL(string: avatarURLString)
                gravatarImageView.kf.setImage(with: imageURL)
            }
            roundedCorner()
}
}
}

实际上,我收到了用户的所有反馈,我希望用户在最顶层的单元格反馈,以便我可以实现编辑和删除反馈功能。

swift uitableview
2个回答
0
投票

如果我说得对,你想在tableView上引入一个不同的单元格,这个单元格与其他单元格不同,放在顶部,对吗?这就是说,你需要第二个UITableViewCell,在这之后,在方法中:cellForRowAt做

if indexPath.row == 0 {
   let cell = tableView.dequeReusableCell(withReuseIdentifier: TopCellId, for indexPath) as! TopCell

//here you update NewCell's properties with your code

return cell
} else {
  let cell = tableView.dequeReusableCell(withReuseIdentifier: RegularCellId, for: indexPath) as! RegularCell

// Here you update the cell which will be used for the rest of the tableView

return cell
}

可能你也可以使用TableViewHeader,但你的问题有点令人困惑。


0
投票

为表创建两个部分:

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

    return 2

}

每个部分的行数彼此独立:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    switch section {

    case 0:
        return feedbacks.count

    case 1:
        return someArray.count

    default:
        return 0

    }

}

然后加载你的单元格:

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

    let p = indexPath.row

    switch indexPath.section {

    case 0:

        let cell = FeedbackTableViewCell()
        cell.someLabel.text = feedbacks[p].someProperty
        return cell

    case 1:

        let cell = tableView.dequeueReusableCell(withIdentifier: someReusableCellId, for: indexPath) as! SomeReusableTableViewCell
        cell.someLabel.text = someArray[p].someProperty
        return cell

    default:
        return UITableViewCell()

    }

}

你需要两种不同的细胞类型,因为我认为反馈细胞看起来不像其他细胞。请注意,我没有将反馈单元格出列,我只是实例化了它;这是因为该单元永远不会被重用,所以不要费心注册和排队。

© www.soinside.com 2019 - 2024. All rights reserved.