首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想在单元格的顶部放置一个特定值。

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

Stack Overflow用户
提问于 2019-04-01 02:21:41
回答 2查看 47关注 0票数 0

我希望将来自服务器的特定值放在表视图的顶部,即我希望将来自用户的反馈的第一行放在表视图的顶部

代码语言:javascript
复制
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()
    }
代码语言:javascript
复制
 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()
}
}
}

实际上,我正在从用户那里获得所有反馈,我希望用户反馈位于最上面的单元格,这样我就可以实现编辑和删除反馈功能。

EN

回答 2

Stack Overflow用户

发布于 2019-04-01 02:46:33

如果我没弄错的话,你想在tableView中引入一个不同的单元格,这个单元格将与其他单元格不同,并放在顶部,对吗?也就是说,在完成此操作后,您需要在方法中使用第二个UITableViewCell : cellForRowAt do

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2019-04-01 05:07:16

为表格创建两个部分:

代码语言:javascript
复制
func numberOfSections(in tableView: UITableView) -> Int {

    return 2

}

每个部分的行数相互独立:

代码语言:javascript
复制
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    switch section {

    case 0:
        return feedbacks.count

    case 1:
        return someArray.count

    default:
        return 0

    }

}

然后加载你的单元格:

代码语言:javascript
复制
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()

    }

}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55444018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档