我希望将来自服务器的特定值放在表视图的顶部,即我希望将来自用户的反馈的第一行放在表视图的顶部
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()
}
}
}实际上,我正在从用户那里获得所有反馈,我希望用户反馈位于最上面的单元格,这样我就可以实现编辑和删除反馈功能。
发布于 2019-04-01 02:46:33
如果我没弄错的话,你想在tableView中引入一个不同的单元格,这个单元格将与其他单元格不同,并放在顶部,对吗?也就是说,在完成此操作后,您需要在方法中使用第二个UITableViewCell : cellForRowAt do
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,但你的问题有点令人困惑。
发布于 2019-04-01 05:07:16
为表格创建两个部分:
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()
}
}为此,您需要两种不同的单元格类型,因为我假设反馈单元格看起来与其他单元格不同。请注意,我没有将反馈单元出队,我只是实例化了它;这是因为该单元永远不会被重用,所以不需要注册和排队。
https://stackoverflow.com/questions/55444018
复制相似问题