tableview.reloadData() 不重复 cellForRowAt 函数
tableview.reloadData() does not repeat cellForRowAt function
我正在尝试使用 Swift 4.
创建一个应用程序
table 的目的是使用 UITableViewCell.SelectionStyle.none
如果我的单元格数据在 API 中计数为 0。
后来,我用UIRefreshControl()
通过下拉table来刷新数据。
但是每当我下拉 table 时,数据都会刷新,但单元格的样式没有改变。
我已经尝试过 reloadData()
但它不起作用。该函数不会重新加载 cellForRowAt
.
这是我的 cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "custom") as! AppsCustomCell
cell.imglink = data[indexPath.row].imglink
cell.title = data[indexPath.row].AppName
cell.subtitle = data[indexPath.row].AppDesc
cell.count = String(data[indexPath.row].count)
cell.backgroundColor = UIColor.clear
cell.layoutSubviews()
if data[indexPath.row].boolPress == 0 || data[indexPath.row].count < 1 {
// if false {
cell.selectionStyle = UITableViewCell.SelectionStyle.none
cell.contentView.isOpaque = false
cell.mainImageView.alpha = 0.5
cell.titleView.alpha = 0.5
cell.subtitleView.alpha = 0.5
cell.count = "0"
} else {
cell.count = String(data[indexPath.row].count)
cell.countView.backgroundColor = UIColor.red
cell.countView.textColor = UIColor.white
}
return cell
}
我希望重新加载整个 table,包括每当我使用刷新拉动时单元格的样式
编辑 :这是我刷新 table
的方式
@IBOutlet weak var tableView: UITableView!
var refreshControl = UIRefreshControl()
override func viewDidLoad() {
refreshControl.addTarget(self, action: #selector(refresh), for: UIControl.Event.valueChanged)
tableView.addSubview(refreshControl) // not required when using UITableViewController
}
@objc func refresh() {
// Insert code here to get data from the API.
self.tableView.reloadData()
self.refreshControl.endRefreshing()
}
编辑:这是UI
中的结果
This is the old data
这里是 table如果我使用 UIRefreshControl()
下拉 table 查看
Refreshed Table
您以错误的方式将刷新控件添加到 table 视图。尝试以下:
override func viewDidLoad() {
super.viewDidLoad()
refreshControl.addTarget(self, action: #selector(refresh), for: UIControl.Event.valueChanged)
tableView.refreshControl = refreshControl
}
此外,设置计数大于 0 时的选择样式。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "custom") as! AppsCustomCell
cell.imglink = data[indexPath.row].imglink
cell.title = data[indexPath.row].AppName
cell.subtitle = data[indexPath.row].AppDesc
cell.count = String(data[indexPath.row].count)
cell.backgroundColor = UIColor.clear
cell.layoutSubviews()
if data[indexPath.row].boolPress == 0 || data[indexPath.row].count < 1 {
// if false {
cell.selectionStyle = UITableViewCell.SelectionStyle.none
cell.contentView.isOpaque = false
cell.mainImageView.alpha = 0.5
cell.titleView.alpha = 0.5
cell.subtitleView.alpha = 0.5
cell.count = "0"
} else {
cell.selectionStyle = .default
cell.count = String(data[indexPath.row].count)
cell.countView.backgroundColor = UIColor.red
cell.countView.textColor = UIColor.white
cell.mainImageView.alpha = 1.0
cell.titleView.alpha = 1.0
cell.subtitleView.alpha = 1.0
}
return cell
}
我正在尝试使用 Swift 4.
创建一个应用程序table 的目的是使用 UITableViewCell.SelectionStyle.none
如果我的单元格数据在 API 中计数为 0。
后来,我用UIRefreshControl()
通过下拉table来刷新数据。
但是每当我下拉 table 时,数据都会刷新,但单元格的样式没有改变。
我已经尝试过 reloadData()
但它不起作用。该函数不会重新加载 cellForRowAt
.
这是我的 cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "custom") as! AppsCustomCell
cell.imglink = data[indexPath.row].imglink
cell.title = data[indexPath.row].AppName
cell.subtitle = data[indexPath.row].AppDesc
cell.count = String(data[indexPath.row].count)
cell.backgroundColor = UIColor.clear
cell.layoutSubviews()
if data[indexPath.row].boolPress == 0 || data[indexPath.row].count < 1 {
// if false {
cell.selectionStyle = UITableViewCell.SelectionStyle.none
cell.contentView.isOpaque = false
cell.mainImageView.alpha = 0.5
cell.titleView.alpha = 0.5
cell.subtitleView.alpha = 0.5
cell.count = "0"
} else {
cell.count = String(data[indexPath.row].count)
cell.countView.backgroundColor = UIColor.red
cell.countView.textColor = UIColor.white
}
return cell
}
我希望重新加载整个 table,包括每当我使用刷新拉动时单元格的样式
编辑 :这是我刷新 table
的方式@IBOutlet weak var tableView: UITableView!
var refreshControl = UIRefreshControl()
override func viewDidLoad() {
refreshControl.addTarget(self, action: #selector(refresh), for: UIControl.Event.valueChanged)
tableView.addSubview(refreshControl) // not required when using UITableViewController
}
@objc func refresh() {
// Insert code here to get data from the API.
self.tableView.reloadData()
self.refreshControl.endRefreshing()
}
编辑:这是UI
中的结果This is the old data
这里是 table如果我使用 UIRefreshControl()
Refreshed Table
您以错误的方式将刷新控件添加到 table 视图。尝试以下:
override func viewDidLoad() {
super.viewDidLoad()
refreshControl.addTarget(self, action: #selector(refresh), for: UIControl.Event.valueChanged)
tableView.refreshControl = refreshControl
}
此外,设置计数大于 0 时的选择样式。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "custom") as! AppsCustomCell
cell.imglink = data[indexPath.row].imglink
cell.title = data[indexPath.row].AppName
cell.subtitle = data[indexPath.row].AppDesc
cell.count = String(data[indexPath.row].count)
cell.backgroundColor = UIColor.clear
cell.layoutSubviews()
if data[indexPath.row].boolPress == 0 || data[indexPath.row].count < 1 {
// if false {
cell.selectionStyle = UITableViewCell.SelectionStyle.none
cell.contentView.isOpaque = false
cell.mainImageView.alpha = 0.5
cell.titleView.alpha = 0.5
cell.subtitleView.alpha = 0.5
cell.count = "0"
} else {
cell.selectionStyle = .default
cell.count = String(data[indexPath.row].count)
cell.countView.backgroundColor = UIColor.red
cell.countView.textColor = UIColor.white
cell.mainImageView.alpha = 1.0
cell.titleView.alpha = 1.0
cell.subtitleView.alpha = 1.0
}
return cell
}